Add logging
This commit is contained in:
parent
0b1090035a
commit
01fbdbbfe8
3 changed files with 25 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"extends": "./node_modules/eslint-config/.eslintrc",
|
"extends": "./node_modules/eslint-config/.eslintrc",
|
||||||
"env": {
|
"env": {
|
||||||
"node": true
|
"node": true,
|
||||||
|
"browser": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,9 @@
|
||||||
"compression": "=1.6.2",
|
"compression": "=1.6.2",
|
||||||
"connect-static-file": "=1.1.2",
|
"connect-static-file": "=1.1.2",
|
||||||
"express": "=4.14.0",
|
"express": "=4.14.0",
|
||||||
"helmet": "=2.3.0"
|
"express-winston": "=2.0.0",
|
||||||
|
"helmet": "=2.3.0",
|
||||||
|
"winston": "=2.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "=1.9.0",
|
"eslint": "=1.9.0",
|
||||||
|
|
27
server.js
27
server.js
|
@ -3,33 +3,46 @@ const staticFile = require('connect-static-file');
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
const helmet = require('helmet');
|
const helmet = require('helmet');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const winston = require('winston');
|
||||||
|
const expressWinston = require('express-winston');
|
||||||
|
|
||||||
const PORT = process.env.PORT || 5000;
|
const PORT = process.env.PORT || 5000;
|
||||||
const SERVE_DIR = path.join(__dirname, '/site');
|
const SERVE_DIR = path.join(__dirname, '/site');
|
||||||
const PAGE_404 = path.join(SERVE_DIR, '.404.html');
|
const PAGE_404 = path.join(SERVE_DIR, '.404.html');
|
||||||
|
const EXPRESS_CONFIG = {
|
||||||
const directory = /\/$/;
|
|
||||||
|
|
||||||
const expressConfig = {
|
|
||||||
dotfiles: 'ignore',
|
dotfiles: 'ignore',
|
||||||
index: false,
|
index: false,
|
||||||
redirect: true
|
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());
|
||||||
|
app.use(expressWinston.logger({
|
||||||
|
transports: [
|
||||||
|
new winston.transports.Console({
|
||||||
|
colorize: true
|
||||||
|
})
|
||||||
|
],
|
||||||
|
meta: false,
|
||||||
|
msg: LOGGER_MESSAGE,
|
||||||
|
colorize: true,
|
||||||
|
statusLevels: true
|
||||||
|
}));
|
||||||
|
|
||||||
app.use(function (request, response, next) {
|
app.use(function (request, response, next) {
|
||||||
if (directory.exec(request.url)) {
|
if (request.url.endsWith('/')) {
|
||||||
request.url = path.join(request.url, 'index.html');
|
request.url = path.join(request.url, 'index.html');
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(express.static(SERVE_DIR, expressConfig));
|
app.use(express.static(SERVE_DIR, EXPRESS_CONFIG));
|
||||||
|
|
||||||
app.use(function (request, response, next) {
|
app.use(function (request, response, next) {
|
||||||
response.statusCode = 404;
|
response.statusCode = 404;
|
||||||
|
|
Reference in a new issue