1 Star 5 Fork 3

v1son/duuchinBackNodeJS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.js 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
v1son 提交于 2023-02-23 16:15 . modify
const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors())
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
//自定义错误
app.use((req, res, next) => {
res.cc = function (err, status = 1) {
res.send({
status,
err: err instanceof Error ? err.message : err
})
}
next()
})
// //jsonwebtoken
// const config = require('./config')
// const expressJWT = require('express-jwt')
// app.use(expressJWT({ secret: config.jwtSecretKey }).unless({ path: [/^\/user\//] }))
//.....................
const userRouter = require('./router/user')
app.use('/user', userRouter)
const songRouter = require('./router/song')
app.use('/song', songRouter)
const videoRouter = require('./router/video')
app.use('/video', videoRouter)
const articleRouter = require('./router/article')
app.use('/article', articleRouter)
const recommendRouter = require('./router/recommend')
app.use('/recommend', recommendRouter)
//验证
const joi = require('joi')
app.use(function (err, req, res, next) {
// 数据验证失败
if (err instanceof joi.ValidationError) return res.cc(err)
res.cc(err)
})
app.use((err, req, res, next) => {
if (err instanceof joi.ValidationError) return res.cc(err)
//身份认证失败后的错误
if (err.name === 'UnauthorizedError') return res.cc('身份认证失败! ')
res.cc(err)
})
app.listen(2022, () => {
console.dir("server listening at http://127.0.0.1:2022")
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/cwiyc/duuchin-back-node-js.git
git@gitee.com:cwiyc/duuchin-back-node-js.git
cwiyc
duuchin-back-node-js
duuchinBackNodeJS
master

搜索帮助