相关疑难解决方法(0)

如何在Node.js上的Express.js中获取GET(查询字符串)变量?

我们可以在Node.js中的查询字符串中获取变量,就像我们$_GET在PHP中获取它们一样吗?

我知道在Node.js中我们可以在请求中获取URL.有没有获取查询字符串参数的方法?

node.js query-string express

1115
推荐指数
20
解决办法
112万
查看次数

解析node.js中的查询字符串

在这个"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)

javascript http node.js

78
推荐指数
5
解决办法
11万
查看次数

标签 统计

node.js ×2

express ×1

http ×1

javascript ×1

query-string ×1