Skip to content

Commit

Permalink
feat(express): 新增错误处理文档和demo代码
Browse files Browse the repository at this point in the history
  • Loading branch information
142vip.cn committed Nov 17, 2023
1 parent 0716934 commit d3a774b
Show file tree
Hide file tree
Showing 9 changed files with 919 additions and 0 deletions.
12 changes: 12 additions & 0 deletions code/express/apps/error-handle-demo/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express')
const app = express()
const port = 4000
const appName = require('./package.json').name
app.get('/', async(req, res) => {
console.log('请求进来了...')
throw new Error('手动抛错了!!')
})

app.listen(port, () => {
console.log(`${appName} listening on port ${port}`)
})
32 changes: 32 additions & 0 deletions code/express/apps/error-handle-demo/handle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const port = 4000
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
app.use(methodOverride())


/**
* 定义一些路由逻辑
*/

app.get('/get', async(req, res) => {
console.log('处理业务逻辑')
})

/**
* 最后自定义错误处理
*
*/
app.use((err, req, res, next) => {
console.log('捕获到的错误信息')
console.log(err)
})

app.listen(port, () => {
console.log('app listen on:', port)
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d3a774b

Please sign in to comment.