国内做受网站,商机创业网2021创业,南昌哪里做网站比较好,建筑人才网评的助工【读书后台管理系统】—后端框架搭建#xff08;二#xff09;
一、 Node 简介
Node 是一个基于 V8 引擎的 Javascript 运行环境#xff0c;它使得 Javascript 可以运行在服务端#xff0c;直接与操作系统进行交互#xff0c;与文件控制、网络交互、进程控制等
Chrome …【读书后台管理系统】—后端框架搭建二
一、 Node 简介
Node 是一个基于 V8 引擎的 Javascript 运行环境它使得 Javascript 可以运行在服务端直接与操作系统进行交互与文件控制、网络交互、进程控制等
Chrome 浏览器同样是集成了 V8 引擎的 Javascript 运行环境与 Node 不同的是他们向 Javascript 注入的内容不同Chrome 向 Javascript 注入了 window 对象Node 注入的是 global这使得两者应用场景完全不同Chrome 的 Javascript 所有指令都需要通过 Chrome 浏览器作为中介实现
二、Express 简介
express 是一个轻量级的 Node Web 服务端框架同样是一个人气超高的项目它可以帮助我们快速搭建基于 Node 的 Web 应用
项目初始化
创建项目
mkdir admin-imooc-node
cd admin-imooc-node
npm init -y安装依赖
npm i -S express创建 app.js
const express require(express)// 创建 express 应用
const app express()// 监听 / 路径的 get 请求
app.get(/, function(req, res) {res.send(hello node)
})// 使 express 监听 5000 端口号发起的 http 请求
const server app.listen(5000, function() {const { address, port } server.address()console.log(Http Server is running on http://%s:%s, address, port)
})三、Express 三大基础概念
1️⃣中间件 中间件是一个函数在请求和响应周期中被顺序调用
注意中间件需要在响应结束前被调用
const myLogger function(req, res, next) {console.log(myLogger)next()
}app.use(myLogger)2️⃣路由
应用如何响应请求的一种规则
响应 / 路径的 get 请求
app.get(/, function(req, res) {res.send(hello node)
})响应 / 路径的 post 请求
app.post(/, function(req, res) {res.send(hello node)
})规则主要分两部分
请求方法get、post… 请求的路径/、/user、/.*fly$/…
3️⃣异常处理
通过自定义异常处理中间件处理请求中产生的异常
app.get(/, function(req, res) {throw new Error(something has error...)
})const errorHandler function (err, req, res, next) {console.log(errorHandler...)res.status(500)res.send(down...)
}app.use(errorHandler)四、项目框架搭建
路由
安装 boom 依赖
npm i -S boom创建 router 文件夹创建 router/index.js
const express require(express)
const boom require(boom)
const userRouter require(./user)
const {CODE_ERROR
} require(../utils/constant)// 注册路由
const router express.Router()router.get(/, function(req, res) {res.send(欢迎学习读书后台管理系统)
})// 通过 userRouter 来处理 /user 路由对路由处理进行解耦
router.use(/user, userRouter)/*** 集中处理404请求的中间件* 注意该中间件必须放在正常处理流程之后* 否则会拦截正常请求*/
router.use((req, res, next) {next(boom.notFound(接口不存在))
})/*** 自定义路由异常处理中间件* 注意两点* 第一方法的参数不能减少* 第二方法的必须放在路由最后*/
router.use((err, req, res, next) {const msg (err err.message) || 系统错误const statusCode (err.output err.output.statusCode) || 500;const errorMsg (err.output err.output.payload err.output.payload.error) || err.messageres.status(statusCode).json({code: CODE_ERROR,msg,error: statusCode,errorMsg})
})module.exports router创建 router/use.js
const express require(express)const router express.Router()router.get(/info, function(req, res, next) {res.json(user info...)
})module.exports router创建 utils/constant
module.exports {CODE_ERROR: -1
}验证 /user/info
user info...验证 /user/login
{code:-1,msg:接口不存在,error:404,errorMsg:Not Found}
文章转载自: http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.wiitw.com.gov.cn.wiitw.com http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.plqkz.cn.gov.cn.plqkz.cn http://www.morning.tgwfn.cn.gov.cn.tgwfn.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn