NODEJS[19]:http模块_获取请求路径与查询字符串
·
Yin灏
const http = require("http");
const url = require("url");
const server = http.createServer((request, response) => {
let res = url.parse(request.url, true);
// 路径
let pathname = res.pathname;
// 查询字符串
console.log(res);
console.log(pathname);
console.log(res.query.keyword); // h5
response.end("url");
});
server.listen(9000, () => {
cons.log("服务器已经启动了...");
});
另外的方式
const http = require("http");
const url = require("url");
const server = http.createServer((request, response) => {
let url = new URL(request.url, "http://127.0.0.1");
console.log(url.pathname);
console.log(url.searchParams.get('keyword'));
response.end("url");
});
server.listen(9000, () => {
cons.log("服务器已经启动了...");
});