Nodejs
(1) 新建app.js
var http = require('http');
http.createServe(function(request,reponse){
//发送http头部
//http状态值:200
//内容类型:text/plain
response.writeHead(200,{
'Content-Type':'text/plain'
});
response.end('hello world');
}).listen(8888);
//终端打印如下信息
console.log('Server running at http:127.0.0.1:8888/');
启动服务
node app.js
(2) 系统模块
-
nodejs模块系统介绍
一个js文件就是一个模块,一个模块会有一些变量或方法,可以导出这些变量或方法提供给别的模块进行使用,它也可以导入别的模块或方法来使用.
-
导出模块和导入模块
-
导出module.exports
-
导入require
-
(3) npm工具
npm是nodejs模块(一般称为包,一个模块就是一个包)管理工具
- 初始化package.json
npm init //需要手动执行
npm init -y //不需要手动执行
- 安装nodejs模块
//如:
npm i jquery --save
//当node_modules被删,可使用以下方法找回
npm i
- 更新nodejs模块
npm update xxx //简写npm i xxx
- 删除nodejs模块
npm uninstall xxx //简写npm un xxx
- 运行指令(根据package.json里的script来决定)
npm run xxx
(4) 设置淘宝镜像
npm config set registry https://registry.npm.taobao.org
//验证是否安装成功(如成功会显示以下)
npm config get registry
(5) 服务器自动重启
npm i nodemon -g
//运行时使用以下代码来代替node app.js来运行
nodemon app.js