-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from greenroach/dev
error handling
- Loading branch information
Showing
9 changed files
with
169 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
{ | ||
"name": "express-ts-template", | ||
"version": "1.0.0", | ||
"description": "Simple starter template for ExpressJs and TypeScript based on expressjs/generator and TypeScript-Node-Starter", | ||
"main": "server.js", | ||
"scripts": { | ||
"start": "npm run serve", | ||
"build": "npm run build-ts && npm run tslint", | ||
"serve": "node dist/server.js", | ||
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"", | ||
"watch-node": "nodemon dist/server.js", | ||
"build-ts": "tsc", | ||
"watch-ts": "tsc -w", | ||
"tslint": "tslint -c tslint.json -p tsconfig.json", | ||
"debug": "npm run build && npm run watch-debug", | ||
"serve-debug": "nodemon --inspect dist/server.js", | ||
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve-debug\"" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.16.2", | ||
"pug": "^2.0.0-rc.4" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^1.14.11", | ||
"@types/express": "^4.11.0", | ||
"@types/node": "^9.4.0", | ||
"concurrently": "^3.5.1", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.6.2" | ||
} | ||
"name": "express-ts-template", | ||
"version": "1.0.0", | ||
"description": "Simple starter template for ExpressJs and TypeScript based on expressjs/generator and TypeScript-Node-Starter", | ||
"main": "server.js", | ||
"scripts": { | ||
"start": "npm run serve", | ||
"build": "npm run build-ts && npm run tslint", | ||
"serve": "node dist/server.js", | ||
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"", | ||
"watch-node": "nodemon dist/server.js", | ||
"build-ts": "tsc", | ||
"watch-ts": "tsc -w", | ||
"tslint": "tslint -c tslint.json -p tsconfig.json", | ||
"debug": "npm run build && npm run watch-debug", | ||
"serve-debug": "nodemon --inspect dist/server.js", | ||
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve-debug\"" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.16.2", | ||
"http-errors": "^1.7.1", | ||
"morgan": "^1.9.1", | ||
"pug": "^2.0.0-rc.4" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.11.0", | ||
"@types/http-errors": "^1.6.1", | ||
"@types/morgan": "^1.7.35", | ||
"@types/node": "^9.4.0", | ||
"concurrently": "^3.5.1", | ||
"nodemon": "^1.14.11", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.6.2" | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import * as createError from "http-errors"; | ||
|
||
declare type WebError = Error & { status?: number }; | ||
export const errorHandler = (err: WebError, req: Request, res: Response, next: NextFunction) => { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get("env") === "development" ? err : {}; | ||
|
||
console.log(123); | ||
// render the error page | ||
res.status(err.status || 500); | ||
res.render("error", { title: err.name, message: err.message }); | ||
}; | ||
|
||
export const errorNotFoundHandler = (req: Request, res: Response, next: NextFunction) => { | ||
next(createError(404)); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,40 @@ | ||
import { app } from "./app"; | ||
|
||
const server = app.listen(app.get("port"), () => { | ||
console.log(("Server running at http://localhost:%d in %s mode"), | ||
app.get("port"), app.get("env")); | ||
console.log("Press CTRL-C to stop\n"); | ||
}); | ||
const port = app.get("port"); | ||
|
||
const server = app.listen(port, onListening); | ||
server.on("error", onError); | ||
|
||
function onError(error: any) { | ||
if (error.syscall !== "listen") { | ||
throw error; | ||
} | ||
|
||
const bind = typeof port === "string" | ||
? "Pipe " + port | ||
: "Port " + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case "EACCES": | ||
console.error(bind + " requires elevated privileges"); | ||
process.exit(1); | ||
break; | ||
case "EADDRINUSE": | ||
console.error(bind + " is already in use"); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
function onListening() { | ||
const addr = server.address(); | ||
const bind = typeof addr === "string" | ||
? "pipe " + addr | ||
: "port " + addr.port; | ||
console.log("Listening on " + bind); | ||
} | ||
|
||
export default server; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extends layout | ||
|
||
block content | ||
h1= title | ||
p= message |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,18 +37,28 @@ | |
"@types/node" "*" | ||
"@types/range-parser" "*" | ||
|
||
"@types/express@^4.11.0": | ||
"@types/express@*", "@types/express@^4.11.0": | ||
version "4.16.0" | ||
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.16.0.tgz#6d8bc42ccaa6f35cf29a2b7c3333cb47b5a32a19" | ||
dependencies: | ||
"@types/body-parser" "*" | ||
"@types/express-serve-static-core" "*" | ||
"@types/serve-static" "*" | ||
|
||
"@types/http-errors@^1.6.1": | ||
version "1.6.1" | ||
resolved "https://registry.yarnpkg.com/@types/http-errors/-/http-errors-1.6.1.tgz#367744a6c04b833383f497f647cc3690b0cd4055" | ||
|
||
"@types/mime@*": | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.0.tgz#5a7306e367c539b9f6543499de8dd519fac37a8b" | ||
|
||
"@types/morgan@^1.7.35": | ||
version "1.7.35" | ||
resolved "https://registry.yarnpkg.com/@types/morgan/-/morgan-1.7.35.tgz#6358f502931cc2583d7a94248c41518baa688494" | ||
dependencies: | ||
"@types/express" "*" | ||
|
||
"@types/node@*": | ||
version "10.12.0" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.0.tgz#ea6dcbddbc5b584c83f06c60e82736d8fbb0c235" | ||
|
@@ -229,6 +239,12 @@ base@^0.11.1: | |
mixin-deep "^1.2.0" | ||
pascalcase "^0.1.1" | ||
|
||
basic-auth@~2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a" | ||
dependencies: | ||
safe-buffer "5.1.2" | ||
|
||
binary-extensions@^1.0.0: | ||
version "1.12.0" | ||
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.12.0.tgz#c2d780f53d45bba8317a8902d4ceeaf3a6385b14" | ||
|
@@ -936,6 +952,16 @@ [email protected], http-errors@~1.6.2, http-errors@~1.6.3: | |
setprototypeof "1.1.0" | ||
statuses ">= 1.4.0 < 2" | ||
|
||
http-errors@^1.7.1: | ||
version "1.7.1" | ||
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.1.tgz#6a4ffe5d35188e1c39f872534690585852e1f027" | ||
dependencies: | ||
depd "~1.1.2" | ||
inherits "2.0.3" | ||
setprototypeof "1.1.0" | ||
statuses ">= 1.5.0 < 2" | ||
toidentifier "1.0.0" | ||
|
||
[email protected]: | ||
version "0.4.23" | ||
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" | ||
|
@@ -1363,6 +1389,16 @@ mkdirp@^0.5.0, mkdirp@^0.5.1: | |
dependencies: | ||
minimist "0.0.8" | ||
|
||
morgan@^1.9.1: | ||
version "1.9.1" | ||
resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.9.1.tgz#0a8d16734a1d9afbc824b99df87e738e58e2da59" | ||
dependencies: | ||
basic-auth "~2.0.0" | ||
debug "2.6.9" | ||
depd "~1.1.2" | ||
on-finished "~2.3.0" | ||
on-headers "~1.0.1" | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||
|
@@ -1521,6 +1557,10 @@ on-finished@~2.3.0: | |
dependencies: | ||
ee-first "1.1.1" | ||
|
||
on-headers@~1.0.1: | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.1.tgz#928f5d0f470d49342651ea6794b0857c100693f7" | ||
|
||
once@^1.3.0: | ||
version "1.4.0" | ||
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" | ||
|
@@ -2057,7 +2097,7 @@ static-extend@^0.1.1: | |
define-property "^0.2.5" | ||
object-copy "^0.1.0" | ||
|
||
"statuses@>= 1.4.0 < 2": | ||
"statuses@>= 1.4.0 < 2", "statuses@>= 1.5.0 < 2": | ||
version "1.5.0" | ||
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c" | ||
|
||
|
@@ -2185,6 +2225,10 @@ to-regex@^3.0.1, to-regex@^3.0.2: | |
regex-not "^1.0.2" | ||
safe-regex "^1.1.0" | ||
|
||
[email protected]: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.yarnpkg.com/token-stream/-/token-stream-0.0.1.tgz#ceeefc717a76c4316f126d0b9dbaa55d7e7df01a" | ||
|