-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (56 loc) · 1.86 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
import dotenv from 'dotenv';
import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import winston from 'winston';
import user from './src/routes/index.js';
const app = express();
dotenv.config();
// parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// parse application/json
app.use(express.json());
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(
morgan(':method :url :status :res[content-length] - :response-time ms')
);
app.use(user);
const port = process.env.PORT || 3000;
const mongodb_uri = process.env.MONGODB_URI;
const mongooseConnectionOptions = { useUnifiedTopology: true };
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
})
);
}
app.listen(port, () => logger.info(`App listening on ${port}`));
const connect = () => {
return mongoose
.connect(mongodb_uri, mongooseConnectionOptions)
.then(() => logger.info(`Connected to database successfully...`));
};
await connect();
// app.get('/', (req, res) => {
// res.status(200).json({ status: 'OK', message: 'Welcome' });
// });