Node JS
模块化
解决依赖关系 命名空间 代码组织的问题
CommonJS 规范
浏览器没有实现/遵守该规范
module.exports={
A,
B
}
或
exports.A=A;
exports.B=B;
如果要使用ES6的模块化方式,需要在package.json中设置"type": "module"
npm & yarn
npm init npm install 包名 –g (uninstall,update)
npm install 包名 --save-dev (uninstall,update)
npm list -g (不加-g,列举当前目录下的安装包)
npm info 包名(详细信息) npm info 包名 version(获取最新版本)
npm install md5@1(安装指定版本) npm outdated( 检查包是否已经过时)
开始新项目
yarn init
添加依赖包
yarn add [package]
yarn add [package]@[version]
yarn add [package] --dev
升级依赖包
yarn upgrade [package]@[version]
移除依赖包
yarn remove [package]
安装项目的全部依赖
yarn install
内置模块
http模块
const http=require('http')
// 创建本地服务器来从其接收数据
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));//一定要有end否则浏览器会超时等待
});
server.listen(8000);