Skip to content

Commit 4885a8c

Browse files
author
微信公众号:储凡
authored
Merge pull request #118 from chufan443/feat/error-handler
2 parents 0716934 + d3a774b commit 4885a8c

File tree

9 files changed

+919
-0
lines changed

9 files changed

+919
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const express = require('express')
2+
const app = express()
3+
const port = 4000
4+
const appName = require('./package.json').name
5+
app.get('/', async(req, res) => {
6+
console.log('请求进来了...')
7+
throw new Error('手动抛错了!!')
8+
})
9+
10+
app.listen(port, () => {
11+
console.log(`${appName} listening on port ${port}`)
12+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const express = require('express')
2+
const app = express()
3+
const bodyParser = require('body-parser')
4+
const methodOverride = require('method-override')
5+
const port = 4000
6+
app.use(bodyParser.urlencoded({
7+
extended: true
8+
}))
9+
app.use(bodyParser.json())
10+
app.use(methodOverride())
11+
12+
13+
/**
14+
* 定义一些路由逻辑
15+
*/
16+
17+
app.get('/get', async(req, res) => {
18+
console.log('处理业务逻辑')
19+
})
20+
21+
/**
22+
* 最后自定义错误处理
23+
*
24+
*/
25+
app.use((err, req, res, next) => {
26+
console.log('捕获到的错误信息')
27+
console.log(err)
28+
})
29+
30+
app.listen(port, () => {
31+
console.log('app listen on:', port)
32+
})
74.9 KB
Loading

0 commit comments

Comments
 (0)