在浏览器中,不同的请求应该作出不同的响应
我们可以从请求req中的url获得请求的内容
然后我们就可以通过判断请求的url来做响应
代码如下:
//根据用户的不同请求,服务器做出不同的响应 // //1.加载http模块 // var http=require('http'); //2.创建http服务,监听 http.createServer(function(req,res){ //获取用户请求req.url //console.log(req.url); //结束响应 //res.end(); res.setHeader('Content-Type','text/plain;charset=utf-8'); //通过req.url获取用户请求的路径,获得不同的响应 if(req.url==='/'||req.url==='/index') res.end('hello index'); else if(req.url==='/login') res.end('hello login'); else if(res.url==='/list') res.end('hello list'); else res.end('404'); }).listen(8080,function(){ console.log('http://localhost:8080'); })