1、新建文件
初始化 npm init
安装express npm install express -save
创建服务
1 //引入express模块 2 const express = require('express') 3 // 创建express实例---创建服务器 4 const app = express() 5 //最简单:默认路径、响应内容 6 app.get('/',(req,res)=>{ 7 res.send('hello express !!') 8 }) 9 // 监听端口 10 app.listen(3000) 11 12 console.log('服务已启动') 13 console.log('请打开网站 localhost:3000')
启动服务 node index.js
2、服务静态资源
新建public,存放一些静态资源
express.static() 访问静态资源路径
__dirname 当前根目录路径 ,
app.use(expres.static(path.join(_dirname,'public')))
3、post请求
接收参数需安装bodyParser:npm install body-parser
路由式响应:路由、对应响应
1 const express = require('express') 2 const bodyParser = require('body-parser') //body-parser接收参数请求 3 4 const app = express() 5 //app.user()方法,使用中间件 6 //bodyParser.urlencoded() 默认解析任意类型; false会解析位字符串 7 app.use(bodyParser.urlencoded({ extended:false })) 8 app.use(bodyParser.json()) //解析json 9 app.post('/add',(req,res)=>{ 10 res.send(req.query) 11 }) 12 app.get('/index',(req,res)=>{ 13 res.send(req.query) //响应:返回请求的参数 14 }) 15 16 app.listen(3000) 17 console.log('服务已启动')
启动 node index.js
4、中间件:(非常洋葱)
一个get请求,可以多个中间件处理,通过next方法
1 const express = require('express') 2 3 const app = express() 4 5 app.get('/index',(req,res,next)=>{ 6 req.name = 'nnnn' 7 next() //交给下一个去处理 8 }) 9 10 app.get('/index',(req,res)=>{ 11 res.send(req.name) 12 }) 13 14 app.listen(3000) 15 console.log('服务已启动')
中间件按顺序处理,(如登录拦截--有登录才能访问)
1 const express = require('express') 2 const bodyParser = require('body-parser') 3 4 const app = express() 5 6 app.use((req,res,next)=>{ 7 req.name="中间件" 8 console.log("0") 9 next() 10 }) 11 app.get("/index",(req,res,next)=>{ 12 13 console.log("1") 14 next() 15 }) 16 app.use("/index",(req,res,next)=>{ 17 console.log("0.1") 18 next() 19 }) 20 app.get("/index",(req,res,next)=>{ 21 console.log("1.1") 22 res.send(req.name) 23 }) 24 25 app.listen(3000) 26 console.log('服务器已经启动') 27 /*结果 28 0 29 1 30 0.1 31 1.1 32 */
5、模板
安装 npm install express-art-template
1 const express = require('express') 2 const path = require('path') 3 const app = express() 4 //注册模板,后缀名art 5 app.engine('art',require('express-art-template')) 6 //设置模板存放位置, views是键 kkk是存放目录 7 app.set('views',path.join(__dirname,'kkk')) 8 //设置默认模板的后缀名:art 9 app.set('view engine','art') 10 // 前提:默认后缀名、存放位置; 11 // res.render()渲染模板,地址、内容 12 app.get('/index',(req,res)=>{ 13 res.render('index',{ 14 msg:'访问首页的信息', 15 abc:'模板:渲染的内容' 16 }) 17 }) 18 app.listen(3000) 19 console.log('服务器已经启动')
6、 路由对象
通过路由对象来拆分路由
存在多个路由对象,将其封装统一引入。
1 const express = require('express') 2 const app = express() 3 4 //创建路由对象 5 const user= express.Router() 6 // 二级路由 /user/index 7 user.get('/index',(req,res)=>{ 8 res.send('访问用户--首页') 9 }) 10 //继续写二级路由 /user/list 11 user.get('/list',(req,res)=>{ 12 res.send('访问用户列表') 13 }) 14 15 // 请求地址--匹配--路由对象 16 app.use('/user',user) 17 18 app.listen(3000) 19 console.log('服务器已经启动')
7、express 生成器
安装: npx express-generator
启动: npm start
1 const express = require('express') 2 const path = require('path') 3 const app = express() 4 //注册模板,后缀名art 5 app.engine('art',require('express-art-template')) 6 //设置模板存放位置, views是键 kkk是存放目录 7 app.set('views',path.join(_dirname,'kkk')) 8 //设置默认模板的后缀名:art 9 app.set('view engine','art') 10 11 app.get('/index',()=>{ 12 res.render('index',{ 13 msg:'访问首页的信息', 14 abc:'模板:渲染的内容' 15 }) 16 }) 17 18 app.listen(3000) 19 console.log('服务器已经启动')