From 2079417e1f8f38c286604027443800289dfc2487 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Fri, 14 Dec 2018 14:14:51 -0800 Subject: [PATCH] Change listen process --- bin/www | 86 ---------------------------------------------------- package.json | 2 +- src/app.ts | 8 ++++- 3 files changed, 8 insertions(+), 88 deletions(-) delete mode 100755 bin/www diff --git a/bin/www b/bin/www deleted file mode 100755 index 6efe33a3..00000000 --- a/bin/www +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env node - -/** - * Module dependencies. - */ - -const app = require("../dist/app"); -const debug = require("debug")("sources:server"); -const http = require("http"); - -/** - * Get port from environment and store in Express. - */ - -const port = normalizePort(process.env.PORT || "3000"); -app.set("port", port); - -/** - * Create HTTP server. - */ - -const server = http.createServer(app); - -/** - * Listen on provided port, on all network interfaces. - */ - -server.listen(port); -server.on("error", onError); -server.on("listening", onListening); - -/** - * Normalize a port into a number, string, or false. - */ - -function normalizePort(val) { - const port = parseInt(val, 10); - - if (isNaN(port)) { - // named pipe - return val; - } - - if (port >= 0) { - // port number - return port; - } - - return false; -} - -/** - * Event listener for HTTP server "error" event. - */ - -function onError(error) { - 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; - } -} - -/** - * Event listener for HTTP server "listening" event. - */ - -function onListening() { - const addr = server.address(); - const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; - debug("Listening on " + bind); -} diff --git a/package.json b/package.json index cac8436f..e8b15de7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ ], "scripts": { "prestart": "tsc && npm run static-files && parcel build src/views/index.html --out-dir ./dist/views/static", - "start": "node ./bin/www", + "start": "node dist/app.js", "dev": "concurrently 'nodemon' 'parcel watch src/views/index.html --no-autoinstall --out-dir ./dist/views/static' 'npm run static-files:watch' --names '💻,⚛️,📦' --prefix name", "test": "NODE_ENV='test' jest --forceExit", "create-data": "ts-node ./scripts/create-data.ts", diff --git a/src/app.ts b/src/app.ts index 2b37a3d6..e95170d0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -51,4 +51,10 @@ app.use(express.static('dist/views/static')) app.use(notFoundHandler) app.use(errorHandler) -module.exports = app +app.listen(process.env.PORT || 3000, () => { + console.log( + `App is running at http://localhost:${process.env.PORT || + 3000} in ${app.get('env')} mode.` + ) + console.log('Press CTRL-C to stop.\n') +})