a.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a href="http://localhost:8080/content.html?id=2&title=这是文章标题">进入详情页</a>
</body>
</html>
server.js:
const http = require("http");
const url = require("url");
const querystring = require('querystring');
const server = http.createServer((req,res)=>{
const reqUrl = req.url;
const queryUrl = url.parse(reqUrl).query;
console.log( querystring.parse(queryUrl) );
//也可以直接用 const queryUrl = url.parse(reqUrl,true).query;
//这样就不用再querystring.parse(queryUrl)了
//如果用了express框架可以直接req.body,就不用进行这么复杂的parse了
res.end();
})
server.listen(8080);
首先在终端跳转到server.js所在文件夹,输入命令node server.js启动服务
此时在浏览器访问a.html点击a链接,在nodejs终端中就会 console.log出参数的json对象