我们可以在Node.js中的查询字符串中获取变量,就像我们$_GET在PHP中获取它们一样吗?
我知道在Node.js中我们可以在请求中获取URL.有没有获取查询字符串参数的方法?
在这个"Hello World"示例中:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
Run Code Online (Sandbox Code Playgroud)
如何从查询字符串中获取参数?
http://127.0.0.1:8000/status?name=ryan
Run Code Online (Sandbox Code Playgroud)
在文档中,他们提到:
node> require('url').parse('/status?name=ryan', true)
{ href: '/status?name=ryan'
, search: '?name=ryan'
, query: { name: …Run Code Online (Sandbox Code Playgroud)