NodeJS的HTTP模块 URL模块 SuPervisor工具

简介: NodeJS的HTTP模块 URL模块 SuPervisor工具

HTTP模块 URL模块 supervisor工具

安装nodejs
vscode安装插件node-snippets 可作代码提示

HTTP模块

书写第一个程序

// 快捷方式 node-http-server
var http = require('http');//nodejs内置模块直接引入使用即可
//创建服务
http.createServer(function (request, response) {
    // request获取url传递的信息
    // response 相应给浏览器的信息
    // 响应头
  response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); 
  res.write('你好nodejs')//页面显示内容
 
  //结束响应
  response.end('Hello World');//也可以让页面增加内容
//   write和send都有内容 那么一块显示
}).listen(8081);//监听端口

console.log('Server running at http://127.0.0.1:8081/');

URL模块

地址栏参数:http://localhost:8081/?id=11
var http=require('http');//引入内置http模块
var url=require('url'); //引入内置URL模块
http.createServer((req,res)=>{
    res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}) 
    console.log(url.parse(req.url))
     console.log(url.parse(req.url).query) //id=11
     console.log(url.parse(req.url,true).query); //加true为Object对象形式{ id: '11' }
     res.write('word')
     res.end()
}).listen(8081)

运行
浏览器地址添加参数
在这里插入图片描述
node xx.js运行后打印的参数

获取query参数: console.log(url.parse(req.url).query)//id=11

在这里插入图片描述

注意:

打印url的时候会出现一个地址栏和/favicon.ico我们取地址栏的时候可以做一个判断
1. [Object: null prototype] { id: '11', name: "'小猪'" }
2./favicon.ico
假设地址栏是http://localhost:8081/?id=11&name='小明'
var  http=require('http');
var  url=require('url');
http.createServer((req,res)=>{
   //[Object: null prototype] { id: '11', name: "'小猪'" }  /favicon.ico
    console.log(req.url);
    if(req.url!=='/favicon.ico'){
        var  urlrequest=url.parse(req.url,true).query;
        console.log(urlrequest)//[Object: null prototype] { id: '11', name: "'小猪'" }
    }
    res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})
    res.write('dd');
    res.end()
}).listen(8081)

supervisor工具使用

解决每次书写代码运行都需要node xxx.js

**1.建议先安装cnpm 防止npm安装失败 下面的命令都是cmd全局安装(win+R)
$ npm install -g cnpm --registry= https://registry.npmmirror.com
2.npm install -g supervisor**

代码终端使用:supervisor xx.js 代替node xx.js即可

相关文章
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
27 0
|
2月前
|
机器学习/深度学习 前端开发 JavaScript
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
43 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
7天前
|
域名解析 网络协议 JavaScript
【Node系列】node工具模块
Node.js有多个内置的工具模块,这些模块提供了用于执行各种任务的功能。
18 2
|
1月前
|
JavaScript 前端开发
Node.js之path路径模块
Node.js之path路径模块
|
1月前
|
JavaScript
Node.js之http模块
Node.js之http模块
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
108 1
|
2月前
|
监控 JavaScript 前端开发
Node.js:JavaScript世界的全能工具
Node.js:JavaScript世界的全能工具
78 0
|
2月前
|
资源调度 JavaScript 关系型数据库
Node.js【文件系统模块、路径模块 、连接 MySQL、nodemon、操作 MySQL】(三)-全面详解(学习总结---从入门到深化)
Node.js【文件系统模块、路径模块 、连接 MySQL、nodemon、操作 MySQL】(三)-全面详解(学习总结---从入门到深化)
33 0
|
2月前
|
JavaScript 前端开发 API
Node.js 工具库 yeoman 的作用介绍
Node.js 工具库 yeoman 的作用介绍
32 0
|
3月前
|
JavaScript 前端开发 Unix
NodeJS文件系统遍历工具:fast-glob
NodeJS文件系统遍历工具:fast-glob
83 0