Convert to CLI

This commit is contained in:
Jake Howard 2017-01-09 20:34:16 +00:00
parent f2fe3a301b
commit 6955a2af14
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 30 additions and 34 deletions

View file

@ -1,8 +1,11 @@
{ {
"name": "host-container", "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": "server.js",
"bin": {
"tstatic": "./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 server.js tests/**.js",
@ -10,17 +13,17 @@
"mocha": "NODE_ENV=test mocha tests/**.test.js" "mocha": "NODE_ENV=test mocha tests/**.test.js"
}, },
"engines": { "engines": {
"node": "5.11.1" "node": "6.9.4"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/RealOrangeOne/host-container.git" "url": "git+https://github.com/RealOrangeOne/tstatic.git"
}, },
"author": "Jake Howard <git@theorangeone.net>", "author": "Jake Howard <git@theorangeone.net>",
"bugs": { "bugs": {
"url": "https://github.com/RealOrangeOne/host-container/issues" "url": "https://github.com/RealOrangeOne/tstatic/issues"
}, },
"homepage": "https://github.com/RealOrangeOne/host-container#readme", "homepage": "https://github.com/RealOrangeOne/tstatic#readme",
"dependencies": { "dependencies": {
"compression": "=1.6.2", "compression": "=1.6.2",
"connect-static-file": "=1.1.2", "connect-static-file": "=1.1.2",

View file

@ -6,38 +6,29 @@ const path = require('path');
const winston = require('winston'); const winston = require('winston');
const expressWinston = require('express-winston'); const expressWinston = require('express-winston');
const IN_TEST = process.env.NODE_ENV === 'true';
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
const SERVE_DIR = path.join(__dirname, '/site'); const SERVE_DIR = process.argv[process.argv.length - 1] || 'site/'
const PAGE_404 = path.join(SERVE_DIR, '.404.html'); const PAGE_404 = path.join(SERVE_DIR, '.404.html');
const EXPRESS_CONFIG = {
dotfiles: 'ignore',
index: false,
redirect: true
};
const LOGGER_MESSAGE = '{{ req.url }}'
.concat('status:{{ res.statusCode }} ')
.concat('useragent:{{ req.headers["user-agent"] }} ')
.concat('time:{{ res.responseTime }}ms');
const app = express(); const app = express();
app.use(compression({ level: 9 })); app.use(compression({ level: 9 }));
app.use(helmet()); app.use(helmet());
if (IN_TEST) {
console.log('Enabling Logging...'); app.use(expressWinston.logger({
app.use(expressWinston.logger({ transports: [
transports: [ new winston.transports.Console({
new winston.transports.Console({ colorize: true
colorize: true })
}) ],
], meta: false,
meta: false, msg: '{{ req.url }} '
msg: LOGGER_MESSAGE, .concat('status:{{ res.statusCode }} ')
colorize: true, .concat('useragent:{{ req.headers["user-agent"] }} ')
statusLevels: true .concat('time:{{ res.responseTime }}ms'),
})); colorize: true,
} statusLevels: true
}));
app.use(function (request, response, next) { app.use(function (request, response, next) {
if (request.url.endsWith('/')) { if (request.url.endsWith('/')) {
@ -46,7 +37,11 @@ app.use(function (request, response, next) {
next(); next();
}); });
app.use(express.static(SERVE_DIR, EXPRESS_CONFIG)); app.use(express.static(SERVE_DIR, {
dotfiles: 'ignore',
index: false,
redirect: true
}));
app.use(function (request, response, next) { app.use(function (request, response, next) {
response.statusCode = 404; response.statusCode = 404;
@ -55,9 +50,7 @@ app.use(function (request, response, next) {
const server = app.listen(PORT, function () { const server = app.listen(PORT, function () {
if (IN_TEST) { console.log('Server started on port ' + server.address().port);
console.log('Server started on port ' + server.address().port);
}
}); });
module.exports = server; module.exports = server;