-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
51 lines (45 loc) · 1.74 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
require('dotenv').config();
const cors = require('cors');
const bodyParser = require('body-parser');
const express = require('express');
const { format } = require('date-fns');
const app = express();
const { sequelize } = require('./databases/bitkub');
const morgan = require('morgan');
const { LINE } = require('./utils/LINE');
require('console-stamp')(console, {
pattern: 'dd/mm/yyyy HH:MM:ss.l',
colors: {
stamp: 'green',
label: 'white',
}
});
const isProduction = process.env.NODE_ENV === 'Production';
if (isProduction) {
// Normal express config defaults
morgan.token('datetime', () => format(new Date(), 'dd/MM/yyyy HH:mm:ss.SSS'));
morgan.token('remoteIP', (req) => req.headers['x-forwarded-for']); // for display real host instead of container host, need to add more header in nginx
app.use(morgan('[:datetime] [:method] :url :remoteIP :status :response-time ms - :res[content-length]'));
} else {
app.use(cors({
origin: ['http://localhost', 'http://localhost:3000'],
optionsSuccessStatus: 200,
credentials: true,
}));
app.use(morgan('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use('/api', require('./routes/tradingview'));
app.listen(process.env.SERVER_PORT || 3000, async () => {
try {
await sequelize.authenticate();
console.log('Database connection has been established successfully.');
console.log(`Server has started at port ${process.env.SERVER_PORT}`);
if (isProduction) LINE(`Server has started at port ${process.env.SERVER_PORT}`);
} catch (error) {
console.error('Unable to connect to the database:', error);
process.exit(1);
}
});