Restructure

This commit is contained in:
Jake Howard 2017-01-19 17:31:23 +00:00
parent d7c70012e5
commit 1b8b738130
8 changed files with 88 additions and 71 deletions

View file

@ -2,14 +2,14 @@
"name": "tstatic", "name": "tstatic",
"version": "1.0.0", "version": "1.0.0",
"description": "Container to host simple static applications using a node server, so files can be deployed using rsync", "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": { "bin": {
"tstatic": "./server.js" "tstatic": "./src/server.js"
}, },
"scripts": { "scripts": {
"test": "npm run mocha && npm run lint && nsp check", "test": "npm run mocha && npm run lint && nsp check",
"lint": "eslint server.js tests/**.js", "lint": "eslint src/ tests/",
"start": "./server.js", "start": "./src/server.js site/",
"mocha": "NODE_ENV=test mocha tests/**.test.js" "mocha": "NODE_ENV=test mocha tests/**.test.js"
}, },
"engines": { "engines": {

View file

@ -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;

9
src/404.js Normal file
View file

@ -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);
};

17
src/logging.js Normal file
View file

@ -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
});

33
src/server.js Executable file
View file

@ -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;

16
src/static-files.js Normal file
View file

@ -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
});

8
src/utils.js Normal file
View file

@ -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'
};

View file

@ -5,7 +5,7 @@ const fs = require('fs');
describe('Server', function () { describe('Server', function () {
var server; var server;
before(function () { before(function () {
server = require('../server'); server = require('../src/server');
}); });
after(function () { after(function () {