url模块
程序员文章站
2022-07-10 16:19:15
...
nodejs的url模块可以解释请求路径的参数,路径名等数据,根据这些数据可以调到不同的处理方法,达到路由的效果,用法如下:
const http = require('http');
const url = require('url');
http.createServer(function(req, resp){
const urlObj = url.parse(req.url);
resp.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
resp.write('pathName:' + urlObj.pathname + '\n');
resp.write('urlObj:' + JSON.stringify(urlObj));
resp.end();
}).listen(8080);
console.log('server is running at port 8080');