web开发概述(02):请求路径分发
请求路径分发
/*
处理请求路径的分发
http.IncomingMessage
http.ServerResponse
*/
const http = require('http');
http.createServer((req,res) =>{
// res.end('ok');
//req.url 可以获取url中的路径(端口之后的路径)
// res.end(req.url);
if(req.url.startsWith('/index')){
//write向客户端响应内容,可以写多次
res.write('hello');
res.write('<h1>Hello</h1>');
//end方法用来完成响应,只能执行一次
res.end();
}else if(req.url.startsWith('/about')){
res.end('about');
}else{
res.end('no content');
}
}).listen(3000,'172.16.10.141',()=>{
console.log('服务启动……');
});

浙公网安备 33010602011771号