2016-12-19 50 views
-1

不工作我有網址:多參數快速的NodeJS

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

我使用快遞的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 

UPDATE :我得到params的代碼是:

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

認爲你需要分享一些你的代碼,如果你想讓任何人有任何機會給你的意見...... – rckrd

+0

我已經更新了我的代碼來獲得PARAMS –

回答

1

您所描述的錯誤很可能是涉及您如何連接路由器快速應用程序。你上面發佈的代碼片段工作正常。

下面是一個工作示例。

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

+0

謝謝,它的工作! –

1

爲什麼不使用req.parameter,以便可以使用常用字符('?'和'&')的完整上下文?舉例來說,你可以簡單地做:

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

和訪問它像http://localhost:8082/something?host= $ VAR_HOST &名= $ VAR_NAME &描述= $ VAR_DESCRIPTION

+0

我正在使用req.params.blarblar獲取參數 –