Skip to content

Commit

Permalink
Merge pull request #2 from greenroach/dev
Browse files Browse the repository at this point in the history
error handling
  • Loading branch information
greenroach authored Oct 21, 2018
2 parents d591896 + 62b09c1 commit c38eecc
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 43 deletions.
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"protocol": "inspector"
}
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 9229,
"address": "localhost",
"restart": true,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/src"],
"skipFiles": [
"${workspaceFolder}/app/node_modules/**/*.js"
],
"localRoot": "${workspaceRoot}/src",
"remoteRoot": "${workspaceRoot}/src",
"smartStep": true,
"trace": true
},
]
}
66 changes: 35 additions & 31 deletions package.json
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"
}
}
9 changes: 8 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as express from "express";
import * as logger from "morgan";
import * as path from "path";

import { errorHandler, errorNotFoundHandler } from "./middlewares/errorHandler";

// Routes
import { index } from "./routes/index";

// Create Express server
export const app = express();

Expand All @@ -12,5 +14,10 @@ app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "../views"));
app.set("view engine", "pug");

app.use(logger("dev"));

app.use(express.static(path.join(__dirname, "../public")));
app.use("/", index);

app.use(errorNotFoundHandler);
app.use(errorHandler);
4 changes: 2 additions & 2 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Request, Response } from "express";
* GET /
* Home page.
*/
export let index = (req: Request, res: Response) => {
res.render("index", { title: "Express" });
export let index = async (req: Request, res: Response) => {
res.render("index", { title: "Express" });
};
18 changes: 18 additions & 0 deletions src/middlewares/errorHandler.ts
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));
};
41 changes: 36 additions & 5 deletions src/server.ts
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;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"target": "es2017",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
Expand Down
5 changes: 5 additions & 0 deletions views/error.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends layout

block content
h1= title
p= message
48 changes: 46 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c38eecc

Please sign in to comment.