node.js 学习03

node.js学习03


  1. 解决浏览器接收服务端信息之后乱码的问题:
    服务器通过设置http响应报文头,告诉浏览器使用相应的编码 来解析网页。

    res.setHeader('Content','text/html;charset=utf-8');

  2. //根据不同的请求做出不同的响应


let http = require('http'); 
http.createServer(function(req,res){

    //获取用户请求的路径req.url
    // console.log(req.url);
    //结束响应
    // res.end();
    if(req.url==='/'||req.url==='/index'){
        res.end('Hello Index');
    }else if(req.url==='/login'){
        res.end('Hello Login');
    }else{
        res.end('404 NotFond');
    }
}).listen(8080,function(){
    console.log('http://localhost:8080');
})
posted @ 2018-08-10 10:33  .helloWorld  阅读(102)  评论(0编辑  收藏  举报