【01】node 引入http模块和url模块

// 表示引入http模块
const http = require("http");
// 引入url模块
const url = require("url");

/*
创建web服务 :
  request(req)   获取 url 传过来的信息
  response(res)  给浏览器响应信息
*/
http
  .createServer((req, res) => {
    //  http://127.0.0.1:8083?name=李四&age=39        想获取 url 传过来的 name 和 age
    // 设置响应投
    //设置 http 头部,状态码是 200 ,文件类型是 html , 字符集是 utf-8
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" }); //解决乱码问题
    res.write("<head><meta charset=utf-8></head>"); //解决乱码问题
    // console.log(req); //获取req
    console.log(req.url); //获取浏览器访问的地址
    if (req.url != "/favicon.ico") {
      var userInfo = url.parse(req.url, true).query;
      console.log(`姓名:${userInfo.name}--年龄:${userInfo.age}`);
    }
    res.write("内容123");
    // 结束响应
    res.end();
  })
  .listen(8083); //设置响应的端口

console.log("Server running at http://127.0.0.1:8083/");

 

posted @ 2020-11-02 11:22  半遮  阅读(302)  评论(0)    收藏  举报