alextran
alextran

Reputation: 161

Multiple parameter not working in NodeJS Express

I have URL:

POST http:/host/core/api/v1/endpoints/create/:name/:description/:address

I am using Express NodeJS. This url will create an endpoint with properties in parameters. But... when I try request with parameters I received errors:

Error: Not Implemented
   at router.use (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\controllers\api.js:517:14)
   at Layer.handle [as handle_request] (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\layer.js:95:5)
   at trim_prefix (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:312:13)
   at D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:280:7
   at Function.process_params (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:330:12)
   at next (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:271:10)
   at Function.handle (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:176:3)
   at router (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:46:12)
   at Layer.handle [as handle_request] (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\layer.js:95:5)
   at trim_prefix (D:\Study\NodeJS\ProjectEnviromentMonitoring\EnvironmentMonitoring\node_modules\express\lib\router\index.js:312:13)

This is JSON will return if success:

{
   "message": "success",
   "statuscode": 200
}

This is my test:

POST http://localhost:8082/core/api/v1/endpoints/create/endpoint3/abc/def

UPDATE: My code to get params is:

router.route('/endpoints/create/:name/:description/:address').post((req, res) => {
var name = req.params.name;
var description = req.params.description;
var address = req.params.address;
 }

Upvotes: 0

Views: 1164

Answers (2)

rckrd
rckrd

Reputation: 3344

The error you describe is most likely related to how you attach the router the the express app. The code snippet you posted above works fine.

Below is a working example.

var express = require('express')
var app = express();
var router = express.Router();

router.route('/endpoints/create/:name/:description/:address').post((req, res) => {
    var name = req.params.name;
    var description = req.params.description;
    var address = req.params.address;
    console.log({ name: name, desc: description, address: address });
    res.json({
        "message": "success",
        "statuscode": 200
    });
});

app.use(router);
app.listen(3000, function () {
    console.log('Example app listening on port 3000!')
});

Test: curl -X POST http://localhost:3000/endpoints/create/test/hello/world

Upvotes: 3

Nicolas Meinen
Nicolas Meinen

Reputation: 63

Why don't you just use the req.parameter so that you can have a full context with the common characters ('?' and '&')? For instance, you could simply do:

var host = req.param('host');
var name = req.param('name');
var description = req.param('description');

and access it like http://localhost:8082/something?host=$VAR_HOST&name=$VAR_NAME&description=$VAR_DESCRIPTION

Upvotes: 1

Related Questions