From 1b8b73813003a24d68abb4aefff477c10b1fc9ef Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 19 Jan 2017 17:31:23 +0000 Subject: [PATCH] Restructure --- package.json | 8 +++--- server.js | 66 -------------------------------------------- src/404.js | 9 ++++++ src/logging.js | 17 ++++++++++++ src/server.js | 33 ++++++++++++++++++++++ src/static-files.js | 16 +++++++++++ src/utils.js | 8 ++++++ tests/server.test.js | 2 +- 8 files changed, 88 insertions(+), 71 deletions(-) delete mode 100755 server.js create mode 100644 src/404.js create mode 100644 src/logging.js create mode 100755 src/server.js create mode 100644 src/static-files.js create mode 100644 src/utils.js diff --git a/package.json b/package.json index c6c1caa..f367bad 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "tstatic", "version": "1.0.0", "description": "Container to host simple static applications using a node server, so files can be deployed using rsync", - "main": "server.js", + "main": "./src/server.js", "bin": { - "tstatic": "./server.js" + "tstatic": "./src/server.js" }, "scripts": { "test": "npm run mocha && npm run lint && nsp check", - "lint": "eslint server.js tests/**.js", - "start": "./server.js", + "lint": "eslint src/ tests/", + "start": "./src/server.js site/", "mocha": "NODE_ENV=test mocha tests/**.test.js" }, "engines": { diff --git a/server.js b/server.js deleted file mode 100755 index 1d0d241..0000000 --- a/server.js +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env node -const express = require('express'); -const staticFile = require('connect-static-file'); -const compression = require('compression'); -const helmet = require('helmet'); -const path = require('path'); -const winston = require('winston'); -const expressWinston = require('express-winston'); -const opbeat = require('opbeat').start({ - active: process.env.NODE_ENV === 'production' -}); - -const PORT = process.env.PORT || 5000; -let SERVE_DIR; -if (process.env.NODE_ENV === 'test') { - SERVE_DIR = 'site'; -} else { - SERVE_DIR = process.argv[process.argv.length - 1]; -} -const PAGE_404 = path.join(SERVE_DIR, '.404.html'); - -const app = express(); - -app.use(expressWinston.logger({ - transports: [ - new winston.transports.Console({ - colorize: true - }) - ], - meta: false, - msg: '{{ req.url }} ' - .concat('status:{{ res.statusCode }} ') - .concat('useragent:{{ req.headers["user-agent"] }} ') - .concat('time:{{ res.responseTime }}ms'), - colorize: true, - statusLevels: true -})); - -app.use(function (request, response, next) { - if (request.url.endsWith('/')) { - request.url = path.join(request.url, 'index.html'); - } - next(); -}); - -app.use(express.static(SERVE_DIR, { - dotfiles: 'ignore', - index: false, - redirect: true -})); - -app.use(function (request, response, next) { - response.statusCode = 404; - staticFile(PAGE_404)(request, response, next); -}); - - -app.use(compression({ level: 9 })); -app.use(helmet()); -app.use(opbeat.middleware.express()); - -const server = app.listen(PORT, function () { - console.log('Server started on port ' + server.address().port); -}); - -module.exports = server; diff --git a/src/404.js b/src/404.js new file mode 100644 index 0000000..56217c3 --- /dev/null +++ b/src/404.js @@ -0,0 +1,9 @@ +const staticFile = require('connect-static-file'); +const path = require('path'); +const { SERVE_DIR } = require('./utils'); + +const handle404 = staticFile(path.join(SERVE_DIR, '.404.html')); +module.exports = function (request, response, next) { + response.statusCode = 404; + return handle404(request, response, next); +}; diff --git a/src/logging.js b/src/logging.js new file mode 100644 index 0000000..dba9130 --- /dev/null +++ b/src/logging.js @@ -0,0 +1,17 @@ +const winston = require('winston'); +const expressWinston = require('express-winston'); + +module.exports = expressWinston.logger({ + transports: [ + new winston.transports.Console({ + colorize: true + }) + ], + meta: false, + msg: '{{ req.url }} ' + .concat('status:{{ res.statusCode }} ') + .concat('useragent:{{ req.headers["user-agent"] }} ') + .concat('time:{{ res.responseTime }}ms'), + colorize: true, + statusLevels: true +}); diff --git a/src/server.js b/src/server.js new file mode 100755 index 0000000..bdb00fa --- /dev/null +++ b/src/server.js @@ -0,0 +1,33 @@ +#!/usr/bin/env node + +console.log('Starting Server...'); + +const app = require('express')(); +const utils = require('./utils'); + +const compression = require('compression'); +const helmet = require('helmet'); +const opbeat = require('opbeat').start({ + active: utils.IN_PRODUCTION +}); + +const logging = require('./logging'); +const staticFiles = require('./static-files'); +const handle404 = require('./404'); + +// Custom Middleware +app.use(logging); +app.use(staticFiles.indexHandle); +app.use(staticFiles.static); +app.use(handle404); + +// Library +app.use(compression({ level: 9 })); +app.use(helmet()); +app.use(opbeat.middleware.express()); + +const server = app.listen(utils.PORT, function () { + console.log('Server started on ' + server.address().port); +}); + +module.exports = server; diff --git a/src/static-files.js b/src/static-files.js new file mode 100644 index 0000000..f0e02ba --- /dev/null +++ b/src/static-files.js @@ -0,0 +1,16 @@ +const express = require('express'); +const path = require('path'); +const { SERVE_DIR } = require('./utils'); + +module.exports.indexHandle = function (request, response, next) { + if (request.url.endsWith('/')) { + request.url = path.join(request.url, 'index.html'); + } + next(); +}; + +module.exports.static = express.static(SERVE_DIR, { + dotfiles: 'ignore', + index: false, + redirect: true +}); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..ddae159 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,8 @@ +const IN_TEST = process.env.NODE_ENV === 'test'; + +module.exports = { + SERVE_DIR: IN_TEST ? 'site/' : process.argv[process.argv.length - 1], + PORT: process.env.PORT || 5000, + IN_TEST, + IN_PRODUCTION: process.env.NODE_ENV === 'production' +}; diff --git a/tests/server.test.js b/tests/server.test.js index 0caef19..d9cca10 100644 --- a/tests/server.test.js +++ b/tests/server.test.js @@ -5,7 +5,7 @@ const fs = require('fs'); describe('Server', function () { var server; before(function () { - server = require('../server'); + server = require('../src/server'); }); after(function () {