当我添加参数字段时,为什么我的express app.get调用不起作用?

时间:2017-06-16 10:08:22

标签: http express get

我想使用express.js路由查询。当我没有指定参数时,它工作正常。也就是说在我的浏览器中输入'localhost:3000 / submissions'然后弹出我的提交页面。

这是代码:

constructor

但是,当我添加输入参数 -

app.get('/submissions', function(req, res){
    res.render('submissions');
});
  • 它返回'无法获取/提交'。这是否是我将参数附加到输入(即'localhost:3000 / submissions:title = whatever')。

有人能告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:1)

如果您将路线定义为/submission/:title,则其映射的网址如下:

http://localhost:3000/submissions/ASampleTitle

http://localhost:3000/submissions/:title=whatever
or
http://localhost:3000/submissions

请参阅快递' routing document代表以下示例:

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

答案 1 :(得分:0)

这里有各种类型的传递数据......

GET https://localhost:4730/countries  --> No data is passed

appHttps.get('/countries', function(req, res) {
    res.json({"countries":["India,China,USA,Canada,...."]} );
});

获取https://localhost:4730/states?country=india - >查询参数传递

appHttps.get('/states', function(req, res) {
   console.log("Country passed is :"+req.query.country); //Consoles --> india
   res.json({"states":[TamilNadu,Kerala,......]});  
});

获取https://localhost:4730/capital/TamilNadu - > req param传递

appHttps.get('/capital/:state', function(req, res) {
    console.log("State Passed is "+req.params.state);// consoles --> TamilNadu
    res.json({"state":"Chennai"});  

}

POST https://localhost:4730/postData - > req正文传递

req body {“name”:“nofi”}

appHttps.post('/postData', function(req, res) {
    console.log(req.body.name);// Consoles --> nofi
    res.json(req.body);
});