-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
100 lines (88 loc) · 2.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require('dotenv').config()
const express = require('express')
const path = require('path')
const fs = require('fs')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const winston = require('winston')
const expressWinston = require('express-winston')
const debug = require('debug')
const log = debug('app:log')
const error = debug('app:error')
const ENV =
process.env?.NODE_ENV === 'development' ? 'development' : ' production'
const index = require('./routes/index')
const helmet = require("helmet");
const app = express()
const maintenance = process.env.MAINTENANCE
const sunset = process.env.SUNSET
app.use(helmet({
contentSecurityPolicy: false,
crossOriginResourcePolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false,
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.set('trust proxy', 'loopback')
app.locals = {
title: 'placepuppy',
ENV,
maintenance,
sunset
}
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser(''))
app.use(express.static(path.join(__dirname, 'public')))
app.use('/', index)
app.use(
expressWinston.errorLogger({
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
})
)
// // catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('404 Not Found')
err.status = 404
next(err)
})
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'access.log'),
{
flags: 'a',
}
)
// express-winston errorLogger makes sense AFTER the router.
app.use(
expressWinston.errorLogger({
transports: [
new winston.transports.File({
filename: 'error.log',
level: 'error',
}),
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
})
)
const errorController = require('./logic/controllers/error.controller')
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
res.status(err.status || 500)
// render the error page
errorController.showErrorPage(req, res, err)
})
exports.app = app
exports.ENV = ENV