0

I have a node.js restful server that receives API calls

let method = request.query.method;

I setup a list of available APIs

enum apis {
    api_name_1,
    api_name_2
}

I want to switch the method I got into the available APIs

switch (method) {
   case apis.api_name_1:
      response.send("You asked for api 1");
      break;
   case apis.api_name_2:
      response.send("You asked for api 2");
      break;
   default:
      response.send("This method is not supported: " + method);
      break;
}

When calling the API like this: api/process?method=api_name_2 , node.js receives is as "api_name_2" (as string) while apis.api_name_2 is equivalent to 1 (enum). How can I convert the name of the api into a "readable" api code for node.js?

Thanks

Amos
  • 1,321
  • 2
  • 23
  • 44
  • Enums are not native to JS and you didn't mention what you are using for them. Different implementations have different APIs. Also I disagree with your need or use of them for this, why not an object with key values? (Though I also disagree with your idea of matching by the method query instead of using a more flexible and powerful URL based router as is standard to do) – Dominic Mar 24 '17 at 11:59
  • I'm still new to this, if you can direct me to read more about "using a more flexible and powerful URL based router" I'd appreciate it. bear in mind that method is not the only parameter I will need – Amos Mar 24 '17 at 12:06

2 Answers2

1

I am not sure what you are trying to achieve by the enum section. Also while you are comparing you can simply compare the strings in switch cases

Here is the code

var express = require('express')
var app = express()


app.get('/api/process', function(req, res) {
    let method = req.query.method;
    console.log(method)


    switch (method) {
        case 'apis.api_name_1':
            res.send("You asked for api 1");
            break;
        case 'apis.api_name_2':
            res.send("You asked for api 2");
            break;
        default:
            res.send("This method is not supported: " + method);
            break;
    }
})

app.listen(3000, function() {
    console.log('Magic begins on port 3000!')
})

EDITED CODE I have updated to code below, since enums are not native to JS as of now and enum is a reserved word in JS, I have replaced the enum implementation of yours with a object.

var express = require('express')
var app = express()

var apiEnum = {
    api_name_1: 'apis.api_name_1',
    api_name_2: 'apis.api_name_2'
};
app.get('/api/process', function(req, res) {
    let method = req.query.method;
    console.log(method)


    switch (method) {
        case apiEnum.api_name_1:
            res.send("You asked for api 1");
            break;
        case apiEnum.api_name_2:
            res.send("You asked for api 2");
            break;
        default:
            res.send("This method is not supported: " + method);
            break;
    }
})

app.listen(3000, function() {
    console.log('Magic begins on port 3000!')
})

Now if you hit http://localhost:3000/api/process?method=apis.api_name_2, you will get the desired result

Vishi
  • 108
  • 9
  • Your solution works ofcourse but I thought of making a list of available APIs for other uses and use this list in the switch as well – Amos Mar 24 '17 at 11:56
  • @Amos I have updated the code. Let me know if it helps – Vishi Mar 24 '17 at 13:06
0

You can use the property access operator [] to get the value from the string as mentioned in this question: code = apis[method];.

Community
  • 1
  • 1
Nathan
  • 505
  • 2
  • 8
  • It works but in the ts file I get a red line saying apis.api_name_1 can't work as string (code = apis[method] returns string). In debug, both values are 0 as expected and it works – Amos Mar 24 '17 at 11:57
  • [This answer](http://stackoverflow.com/questions/17380845/how-to-convert-string-to-enum-in-typescript) may provide more insight. – Nathan Mar 24 '17 at 11:59
  • sorry for being slow here but again I'm missing something. I did the following: let methodCode:apis = apis[method]; but I get Type 'string' is not assignable to type 'apis' even though this is equivalent to the answer he got – Amos Mar 24 '17 at 12:26