美文网首页
NodeJs HTTP模块,URL模块,supervisor

NodeJs HTTP模块,URL模块,supervisor

作者: super静_jingjing | 来源:发表于2018-10-24 14:38 被阅读0次
  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

    其他编程语言,需要借用Apache或者Nginx的HTTP的服务器来处理请求;但是nodejs完全不一样,直接引用...

  • nodejs静态资源服务器

    缩写含义 http是nodejs的服务模块 url是url路由模块 fs是文件服务器模块 nodejs服务器的创建...

  • nodejs静态资源服务器

    nodejs静态资源服务器 1、http 是nodejs的服务模块 2、url 是url路由模块 3、fs 是文件...

  • node.js学习日记day1

    nodejs-01 http模块 创建一个http服务器: url模块中的parse方法: 该方法可以把url中包...

  • nodejs 中有哪些常用的内置模块

    path模块nodejs中的path模块用于处理文件和目录的路径url模块在nodejs中url模块是用来解析ur...

  • 周国康-20160809笔记

    HTTP,URL,FS模块 HTTP模块 FS模块 URL模块 作业 构造静态服务Server:解析URL,根据U...

  • 兄弟会8.9号笔记

    node.js模块学习 http 模块 fs 模块 url 模块 http 模块 HTTP http.STA...

  • nodejs模块

    nodejs模块 nodejs系统自带的模块:http:协议请求模块;创建服务器:http.createServe...

  • http 模块

    http 模块起一个本地服务器例子 supervisor 每次修改 nodejs 文件都要重新执行一次 node ...

  • nodejs url模块详解

    nodejs url模块 nodejs中用户url格式化和反格式化模块用于url解析、处理等操作的解决方案 1.u...

网友评论

      本文标题:NodeJs HTTP模块,URL模块,supervisor

      本文链接:https://www.haomeiwen.com/subject/tkqmsftx.html