自定义模块
require——引入其他模块
exports——输出
module——批量输出
自定义模块向外输出
/*
exports.a=12;
exports.b=5;
exports.c=99;
*/
//var a=12;
exports.a=12;
获取自定义模块输出内容
const mod=require('mod');
console.log(mod.a);
批量向外输出
module.exports={a: 12, b: 5, c: 99};
npm
npm:NodeJS Package Manager(NodeJS包管理器)
1.统一下载途径
2.自动下载依赖
node_modules——放模块
./
不加./ 必须放在node_modules里面
require
1.如果有"./"
从当前目录找
2.如果没有"./"
先从系统模块
再从node_modules找
自定义模块统一,都放到node_modules里面
npm发布自己的模块
https://www.npmjs.com/
登陆自己的npm账号
npm login
Username: zhf198821
Password:
Email: (this IS public) 349860782@qq.com
Logged in as zhf198821 on https://registry.npmjs.org/.
npm whoami 查看自己的信息
npm init 初始化
Press ^C at any time to quit.
package name: (自定义模块) mytest
version: (1.0.0)
description: 这是我的测试包
entry point: (index.js)
test command:
git repository:
keywords:
生成信息
{
"name": "mytest",
"version": "1.0.0",
"description": "这是我的测试包",
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
npm publish
如果无法发布说明该包名字被其他人占有
跟新后发布据需要重新发布
npm update 包名
express模块
安装
cnpm install express
express模块响应请求
const express=require('express');
var server=express();
server.use('/', function (req.res){
console.log('use了');
});
server.listen(8080);
express中res req
非侵入式 增强
req
原生:
res.write();
res.end();
express:
*res.send();
res.write();
res.end();
send可以发送 json对象
res.send({a:11,b:12})
express中处理get与post
//server.get('/', function (){
//console.log('有GET');
//});
//server.post('/', function (){
//console.log('有POST');
//});
express中使用express-static中间件
server.use(expressStatic('./www'));
express综合练习
const express=require('express');
const expressStatic=require('express-static');
var server=express();
server.listen(8080);
//用户数据
var users={
'blue': '123456',