NodeJs HTTP模块,URL模块,supervisor

  1. 其他编程语言,需要借用Apache或者Nginx的HTTP的服务器来处理请求;但是nodejs完全不一样,直接引用http模块就可以了
    代码也比较简单
const http = require("http");
http.createServer(function(req,res){
    //发送http头部
    //http状态值:200 ok
    // 设置http 头部,状态码是200,文件类型是html,字符集是utf8
    res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
    res.write("你好");
    //结束响应
    res.end();
}).listen(8001);

在控制台启动程序,node XXX.js
然后在浏览器:http://localhost:8081,页面打印出“你好”

  1. URL模块
    该模块其实就是对URL进行一些操作,该模块提供了方法可以方便获取url的各种参数,也可以实现对url的操作。常用的就是parse方法,方法使用如下:
const http = require("http");
const url = require("url");
http.createServer(function(req,res){
    const result = url.parse("http://www.baidu.com?name=zhangsan&age=18",true);
    console.log(result);
    res.end();
}).listen(8001);

parse获取的数据如下:

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=18',
  query: { name: 'zhangsan', age: '18' },
  pathname: '/',
  path: '/?name=zhangsan&age=18',
  href: 'http://www.baidu.com/?name=zhangsan&age=18' }

3.supervisor
不使用supervisor的话,每次修改nodejs的代码都需要重新启动。但是如果使用了supervisor,只需要启动一次,后面修改代码会自动刷新,不需要重启。

npm install -g supervisor
// 启动换成以下方式
supervisor nodejs文件
//之后改代码会自动刷新

你可能感兴趣的:(NodeJs HTTP模块,URL模块,supervisor)