多个参数在NodeJS Express中不起作用

时间:2016-12-19 15:52:20

标签: node.js rest api express

我有网址:

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

我正在使用Express NodeJS。此URL将创建一个包含参数属性的端点。但是......当我尝试使用参数请求时,我收到了错误:

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)

如果成功,这将是JSON:

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

这是我的测试:

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

更新:我获取参数的代码是:

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;
 }

2 个答案:

答案 0 :(得分:1)

为什么不使用req.parameter,以便您可以拥有常用字符的完整上下文('?'和'&') ?例如,您可以这样做:

   public partial class dropCalMig : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Calendars");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Calendars",
                columns: table => new
                {
                    UserCalendarID = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    EventID = table.Column<int>(nullable: false),
                    UserID = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Calendars", x => x.UserCalendarID);
                });
        }
    }

并访问http://localhost:8082/something?host= $ VAR_HOST&amp; name = $ VAR_NAME&amp; description = $ VAR_DESCRIPTION

答案 1 :(得分:1)

您描述的错误很可能与您将Express路由器连接到路由器的方式有关。您在上面发布的代码段工作正常。

以下是一个工作示例。

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!')
});

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