-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (46 loc) · 1.46 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
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const morgan = require('morgan');
const fs = require('fs');
const winston = require('winston');
const cors = require('cors')
const DailyRotateFile = require('winston-daily-rotate-file');
const contactusRouter = require('./routes/contactus');
let logger, Stream;
setUpLogger();
const app = express();
const port = process.env.PORT || 3000;
let pathToLogs = `./logs/http__${new Date('April 9, 2019').toISOString().split('T')[0]}.txt`
app.use(helmet());
app.use(cors({origin: '*', optionsSuccessStatus: 200}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('common', {stream : new Stream }));
// app.use(express.static('client/public'))
app.use('/contactus', contactusRouter);
app.use('*', (err, req, res, next) => {
res.status(404).end('something went wrong');
})
app.listen(port, () => {
console.log('app is listening on port: ' + port);
})
function setUpLogger() {
logger = winston.createLogger({
level: 'error',
transports: [new DailyRotateFile({
level: 'info',
filename: 'http__%DATE%.log',
dirname: './logs/',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
})]
})
Stream = class Stream {
write(data) {
logger.info(data);
}
}
}