最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - app.get and app.post get value in node.js - Stack Overflow

programmeradmin0浏览0评论

I'm using the app.get and app.post functions in Express on Node.js.

I have examples like this:

app.post('/status', function(req, res) {
  if (~packages.STATUSES.indexOf(req.body['status'])) {
    res.status(req.body.status, req.body.message);
    res.jsonp(new packages.Success('status updated'));
  } else {
    res.jsonp(new packages.Error('invalid status'));
  }
});

This will work if I post the data to the sever, and I noticed that it gets the value by

req.body['status'];

What if I use GET and pass the value here? What should I do to get the 'status'?

app.get('/status', function(req, res) {
// how can I get status... var status = ??
  if (~packages.STATUSES.indexOf(status) { //got value
    res.status(req.body.status, req.body.message);
    res.jsonp(new packages.Success('status updated'));
  } else {
    res.jsonp(new packages.Error('invalid status'));
  }
});

Sorry if it sounds dumb but I did some research and couldn't find any examples online. Thanks for your help.

I'm using the app.get and app.post functions in Express on Node.js.

I have examples like this:

app.post('/status', function(req, res) {
  if (~packages.STATUSES.indexOf(req.body['status'])) {
    res.status(req.body.status, req.body.message);
    res.jsonp(new packages.Success('status updated'));
  } else {
    res.jsonp(new packages.Error('invalid status'));
  }
});

This will work if I post the data to the sever, and I noticed that it gets the value by

req.body['status'];

What if I use GET and pass the value here? What should I do to get the 'status'?

app.get('/status', function(req, res) {
// how can I get status... var status = ??
  if (~packages.STATUSES.indexOf(status) { //got value
    res.status(req.body.status, req.body.message);
    res.jsonp(new packages.Success('status updated'));
  } else {
    res.jsonp(new packages.Error('invalid status'));
  }
});

Sorry if it sounds dumb but I did some research and couldn't find any examples online. Thanks for your help.

Share Improve this question edited Oct 2, 2013 at 19:02 hexacyanide 91.8k31 gold badges166 silver badges162 bronze badges asked Oct 2, 2013 at 18:48 jackhaojackhao 3,8674 gold badges24 silver badges44 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

I would strongly suggest to use

req.param('status') //

it looks up req.body/req.query as well as req.params, so with this you can map all three below routes to the same method

app.get('/status',statusHanlder);
app.post('/status', statusHanlder);
app.get('/status/:status', statusHanlder)

var statusHanlder = function(req, res){
  var status = req.param('status')
}

Use req.query.status or req.query['status'] for GET requests.

发布评论

评论列表(0)

  1. 暂无评论