express学习笔记4 - 热更新以及express-boom

我们每次改动代码的时候都要重启项目,现在我们给项目添加一个热更新

复制代码
npm install --save-dev nodemon # or using yarn: yarn add nodemon -D

在package.json添加一行代码

复制代码
"dev": "nodemon ./bin/www"

重启项目 然后随便做改动,刷新页面,如果有变化就成功了,后期我们改动只需要刷新页面就可以了

复制代码
node app.js

接下来安装expres-boom (我一般用yarn,你们可以用npm或者pnpm)

复制代码
yarn add express-boom --S

修改一下刚在router/index.js封装的404代码

引入boom

复制代码
const boom = require('boom')

使用boom

复制代码
router.use((req, res, next) => {
    // console.log(req,res)
    next(boom.notFound('接口不存在'))
  })

代码片段

复制代码
const express = require('express')
const boom = require('boom')

// 注册路由
const router = express.Router() 
 
router.get('/',function(req,res){
    res.send('让我们开始express之旅')
})
/**
 * 集中处理404请求的中间件
 * 注意:该中间件必须放在正常处理流程之后
 * 否则,会拦截正常请求
 */
router.use((req, res, next) => {
    // console.log(req,res)
    next(boom.notFound('接口不存在'))
  })
module.exports = router

刷新页面(http://localhost:8000/user

相关推荐
齐生12 天前
iOS 知识点 - 渲染机制、动画、卡顿小集合
笔记
用户962377954482 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
齐生13 天前
iOS 知识点 - IAP 是怎样的?
笔记
tingshuo29174 天前
D006 【模板】并查集
笔记
tingshuo29175 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者10 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky10 天前
Django入门笔记
笔记·django
勇气要爆发10 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意10 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发10 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain