Last active
August 28, 2018 02:39
-
-
Save sj82516/b2a88e529b12e1f9b4cdcae254b7152d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express') | |
| require('express-async-errors') | |
| var app = express() | |
| var pE = require("./promisifyError") | |
| var rt = express.Router(); | |
| app.get('/', async function (req, res, next) { | |
| await pE(); | |
| }) | |
| app.use("/api", rt) | |
| // 沒有 require('express-async-errors') 就要自己包 try catch | |
| // app.get('/', async function (req, res, next) { | |
| // try{ | |
| // await pE(); | |
| // }catch(error){ | |
| // next(error) | |
| // } | |
| // }) | |
| app.use((req, res, next) => { | |
| console.log("will I be called?", "no") | |
| }) | |
| app.use((err, req, res, next) => { | |
| console.log("will I be called?", "yes") | |
| next(err) | |
| }) | |
| // Error Handling 必須宣告於最後面 | |
| // 且必須是 (err, req, res, next) => {} 四個參數 | |
| // 這也是 Express 蠻奇怪的地方 | |
| app.use((err, req, res, next) => { | |
| console.log("error", err); | |
| res.send(500, "ServerError") | |
| }) | |
| app.listen(3001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment