Trong "Hello World" ví dụ này:Parsing Query String trong Node.js
// 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/");
Làm thế nào tôi có thể nhận được các thông số từ chuỗi truy vấn?
http://127.0.0.1:8000/status?name=ryan
Trong tài liệu hướng dẫn, họ đề cập:
node> require('url').parse('/status?name=ryan', true)
{ href: '/status?name=ryan'
, search: '?name=ryan'
, query: { name: 'ryan' }
, pathname: '/status'
}
Nhưng tôi không hiểu làm thế nào để sử dụng nó. Bất cứ ai có thể giải thích?
Cảm ơn trước
Cảm ơn bạn, tôi thử nghiệm, nó làm việc. Cảm ơn các liên kết. Đoán rằng tôi cần phải tìm hiểu thêm: D –
mẹo hay nhất; thanks :) – gdibble
[Liên kết này] (https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/) giúp tôi nhận ra rằng '[t] anh ta yêu cầu đối tượng là một thể hiện của IncomingMessage' và 'http.IncomingMessage' [có thuộc tính] (https://nodejs.org/api/http.html)' url'. –