javascript Nodejs,Express GET POST 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11662085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:56:31  来源:igfitidea点击:

Nodejs, Express GET POST params

javascriptnode.jsexpress

提问by John Williams

I'm new to Node/Express.. I see GET params can be captured like so:

我是 Node/Express 的新手。我看到可以像这样捕获 GET 参数:

app.get('/log/:name', api.logfunc);

POST like so:

像这样发布:

app.post('/log', ...(form variables available in req.body.)

app.post('/log', ...(表单变量在 req.body 中可用。)

I'm aware of app.all, but is there a single way I can get all the variables for GET and POST when using app.all? (I'm too used to $_REQUEST in php!:)

我知道 app.all,但是有没有一种方法可以在使用 app.all 时获取 GET 和 POST 的所有变量?(我太习惯在 php 中使用 $_REQUEST!:)

thx,

谢谢,

回答by ebohlman

You're dealing with three different methods of parameter-passing:

您正在处理三种不同的参数传递方法:

1) Path parameters, which express's router captures in req.paramwhen you use colon-prefixed components or regex captures in your route. These can be present in both GET and POST requests.

1) 路径参数,req.param当您在路由中使用冒号前缀组件或正则表达式捕获时,express 的路由器会捕获该参数。这些可以出现在 GET 和 POST 请求中。

2) URL query-string parameters, which will be captured in req.queryif you use the express.querymiddleware. These can also be present in both GET and POST requests.

2) URL 查询字符串参数,req.query如果使用express.query中间件会被捕获。这些也可以出现在 GET 和 POST 请求中。

3) Body parameters, which will be captured in req.bodyif you use the express.bodyParsermiddleware. These will only be present in POST requests that have a Content-Typeof "x-www-form-urlencoded".

3)body参数,req.body如果使用express.bodyParser中间件会被捕获。这些只会出现在具有Content-Type“x-www-form-urlencoded”的POST 请求中。

So what you need to do is to merge all three objects (if they exist) into one. There are no native Objectmethods for doing this, but there are lots of popular workarounds. For example, the underscore.jslibrary defines an extendfunction, which would allow you to write

因此,您需要做的是将所有三个对象(如果存在)合并为一个。没有本地Object方法可以执行此操作,但有许多流行的解决方法。例如,underscore.js库定义了一个extend函数,它允许您编写

req.params=_.extend(req.params || {}, req.query || {}, req.body || {}).

If you don't want to use a library and want to roll your own way of extending objects, take a look at this blog post.

如果您不想使用库并希望以自己的方式扩展对象,请查看此博客文章

回答by pirs

Personnaly, i merge req.params, req.body, req.queryin one single object req.propswith Object.assign()

Personnaly,我将req.params, req.body,合并req.query到一个对象req.propsObject.assign()

in ES6:

在 ES6 中:

You just need to write this in your route:

你只需要在你的路线中写下这个:

   app.all('/myroute/:myparam', (req, res, next) => {
        // merge all req data in one
        req.props = Object.assign(req.query, req.params, req.body);
        // optional :
        // delete req.query;
        // delete req.params;
        // delete req.body;
    });

In ES5:

在 ES5 中:

    app.all('/myroute/:myparam', function(req, res, next){
        // merge all req data in one
        req.props = {};
        if(req.query)  for (var attrname in req.query)  { req.props[attrname] = req.query[attrname]; }
        if(req.params) for (var attrname in req.params) { req.props[attrname] = req.params[attrname]; }
        if(req.body)   for (var attrname in req.body)   { req.props[attrname] = req.body[attrname]; }
        // optional :
        // delete req.query;
        // delete req.params;
        // delete req.body;
    });

Now, you can access easily to your GET, POST, PUT params with req.propsin all your routes, be careful about similars name if you decide to delete the old req.

现在,您可以轻松访问req.props所有路由中的GET、POST、PUT 参数,如果您决定删除旧请求,请注意相似名称。

Also, you can do a middleware/functions to make it more useful yet.

此外,您可以做一个中间件/功能,使其更有用。

More about Object.assign(): https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/assign

更多关于Object.assign()https: //developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/assign