nodeJS框架

  1. express4.x[资料多,社区用户多]
  2. koa2[原版人马出品]
  3. egg[国产的nodejs框架]
NodeJs框架主要解决的问题
  • 1.原生NodeJs要处理静态资源访问的问题(CSS,IMG,HTML,IMG);
  • 2.原生Nodejs的路由匹配需要使用字符串或者正则自己去匹配,截取
  • 3.原生NodeJs需要配置环境,比如乱码问题,数据格式问题等等

nodejs框架之express

express操作

  • 安装命令 npm install express -S
  • 查看版本 npm list express
搭建espress的基本环境

新建app.js文件

//导入express模块
const express = require('express');
//通过express()创建一个应用程序app
const app = express();
const path = require('path');
//斜杠来匹配网址的首页
app.get('/',function(req,res){
  //sendFile()必须传入一个绝对路径,给客户端发送一个文件 
 //路径符合,可以加载页面 res.sendFile(path.resolve(__dirname,'index.html'));
  
})

//监听端口号
app.listen(8080,function(){
  console.log('server startup on 8080');
})
路由配置

忽略大小写、自动过滤参数和锚链接信息

//路由匹配
//地址:http://localhost:8080/student/del
//地址:http://localhost:8080/student/del?id=123
//地址:http://localhost:8080/student/del?id=123#abc
//地址:http://localhost:8080/student/DEL?id=123#abc
//以上路径均可以匹配到
app.get('/student/del',function(req,res){})
app.post("地址",function(req,res){});
app.put("地址",function(req,res){});
app.delete("地址",function(req,res){});
app.use()

作用:加载中间间,[一个函数,插件],不需要配路径,不需要指定请求方式

app.use(function(req,res,next){
  //业务处理
})
//处理静态资源
app.use(express.static(__dirname+'/public/'))
//404页面(404处理一定要写在所有路由匹配的最后面,因为它是按顺序执行的)
//前面所有路径都匹配不到,才执行这里的代码
app.use(function(req,res,next){
  res.status(404).sendFile(path.resolve(__dirname,'404.html'))
})
app.all()

不论采取哪一种请求方式,只要地址正确,都能执行app.all()的回调函数

app.all('/student/list',function(req,res){
  res.end("/student/list success");
})
响应函数

作用:服务端给客户端返回的结果函数
sendFile():发送一个文件到客户端

//绝对路径
res.sendFile(path.resolve(__dirname,'index.html'))

end():来自原生Node,表示关闭连接,性能差,中文默认不能解析,不建议使用

res.end("end.....")

json():可以给客户端发送一个json对象或者null,它的Content-Type会自动变成:application/json;

res.json(null);
res.json({name:'frank'})

send():可以给客户端发送Buffer对象,String,Object,Array,Boolean,根据内容动态调整Content-Type

res.send("字符串");
res.send({});
搭建Express脚手架

作用:自动创建一套规范化的项目构架
1.全局安装Express脚手架【-g表示全局安装,每台电脑装一次即可】

npm install express-generator -g

2.用Express脚手架生成一个新项目[my-app为自定义的项目名称]

express --view=ejs my-app
//my-app文件的名字,可更改

3.切换目录并安装依赖包

cd my-app && npm install

4.启动项目

npm start
//默认端口号为3000

5.打开浏览器默认地址:http://localhost:3000

nodeport 制定端口 node默认端口_nodeport 制定端口


//项目创建成功

6.项目目录结构

nodeport 制定端口 node默认端口_前端_02


7.模块化路由

作用:将业务进行模块化划分

(1):在routes下创建一个新的模块文件,列如:shopcart.js

//购物车模块
const express = require('express');
//模块化路由对象
const router = express.Router();
router.get('/',function(req,res){
  res.send([
    {title:"鼠标",price:200},
    {title:"键盘",price:500}
  ]);
})
router.get('/detail',function(req,res){
  res.send({title:"鼠标",price:200})
})
module.exports = router;

(2):在app.js文件引入

//引入模块化路由
var shopcartRouter = require('./routes/shopcart');
//环境配置
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//添加模块化路由(一定要放在环境配置的后面,确保环境配置完成再加载自己的业务代码)
app.use('/shopcart',shopcartRouter);

(3):访问shopcart模块路由的时候,一定要写shopcart前缀

http://localhost:3000/shopcart
//返回结果:[{title:"鼠标",price:200},{title:"键盘",price:500}]

http://localhost:3000/shopcart/detail
//返回结果:{title:"鼠标",price:200}

http://localhost:3000/detail
//忘了写shopcart前缀,会出现404页面