This repository has been archived by the owner on Dec 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added Favicon - Updated Dependencies - Rewrote express server - Updated README
- Loading branch information
Showing
7 changed files
with
490 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Logs | ||
logs | ||
log/ | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,61 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const favicon = require('serve-favicon'); | ||
const logger = require('morgan'); | ||
const morgan = require('morgan'); | ||
const bodyParser = require('body-parser'); | ||
const rfs = require('rotating-file-stream'); | ||
|
||
const app = express(); | ||
const logDirectory = path.join(__dirname, 'log'); | ||
const accessLogStream = rfs('access.log', { | ||
interval: '1d', | ||
path: logDirectory | ||
}); | ||
|
||
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory); | ||
|
||
app.use(logger('dev')); | ||
app.use(morgan('dev', { skip: (req, res) => res.statusCode < 400 })); | ||
app.use(morgan('common', { skip: (req, res) => app.get('env') !== 'production', stream: accessLogStream })); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(express.static(path.join(__dirname, 'dist'))); | ||
app.use(notFound); | ||
app.use(logErrors); | ||
app.use(clientErrorHandler); | ||
app.use(errorHandler); | ||
|
||
app.use((req, res, next) => { | ||
function notFound(req, res, next) { | ||
const err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
} | ||
|
||
app.use((err, req, res, next) => { | ||
// set locals, only providing error in development | ||
function logErrors(err, req, res, next) { | ||
console.error(err.stack); | ||
next(err); | ||
} | ||
|
||
function clientErrorHandler(err, req, res, next) { | ||
if (req.xhr) { | ||
res.status(500).send({ error: 'Something failed!' }); | ||
} else { | ||
next(err); | ||
} | ||
} | ||
|
||
function errorHandler(err, req, res, next) { | ||
if (res.headersSent) { | ||
return next(err); | ||
} | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render('error'); | ||
}); | ||
res.json({ | ||
message: err.message, | ||
error: err | ||
}); | ||
} | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.