如何在 Node.js 上的 Express.js 中获取 GET(查询字符串)变量? [英] How to get GET (query string) variables in Express.js on Node.js?
本文介绍了如何在 Node.js 上的 Express.js 中获取 GET(查询字符串)变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们能否像在 PHP 中的 $_GET
中获取它们一样,在 Node.js 中获取查询字符串中的变量?
Can we get the variables in the query string in Node.js just like we get them in $_GET
in PHP?
我知道在 Node.js 中我们可以获取请求中的 URL.有没有获取查询字符串参数的方法?
I know that in Node.js we can get the URL in the request. Is there a method to get the query string parameters?
推荐答案
在 Express 中,它已经为您完成,您只需使用 req.query:
In Express it's already done for you and you can simply use req.query for that:
var id = req.query.id; // $_GET["id"]
否则,在 NodeJS 中,您可以访问 req.url 和内置的 url
模块到 url.parse 手动:
Otherwise, in NodeJS, you can access req.url and the builtin url
module to url.parse it manually:
var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
这篇关于如何在 Node.js 上的 Express.js 中获取 GET(查询字符串)变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文