-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (68 loc) · 2.1 KB
/
server.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
import express from 'express';
import next from 'next';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import chalk from 'chalk';
import dayjs from 'dayjs';
import open from 'open';
import 'dayjs/locale/id';
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
morgan.token('dateTime', (req, res, tz) => {
return dayjs().locale('id').format('dddd, D MMMM YYYY, HH.mm');
});
app.prepare().then(() => {
const server = express();
const port = process.env.NEXT_PUBLIC_APP_PORT ? process.env.NEXT_PUBLIC_APP_PORT : 3000;
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
// Disabling x-powered-by in response headers
server.set('x-powered-by', false);
// Access log
if (dev) {
server.use(
morgan(
`[ ${chalk.blue('request')} ] ${chalk.greenBright(':method')} ${chalk.yellowBright(
':url'
)} ${chalk.magentaBright(':status')} ${chalk.cyanBright(':response-time ms')}`
)
);
} else {
server.use(
morgan(
`${chalk.blueBright('[:dateTime]')} ${chalk.greenBright(':method')} ${chalk.yellowBright(
':url'
)} ${chalk.magentaBright(':status')} ${chalk.cyanBright(':response-time ms')} ":user-agent"`
)
);
}
server.all('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
const appName = process.env.NEXT_PUBLIC_APP_NAME ? process.env.NEXT_PUBLIC_APP_NAME : 'Application';
if (err) {
console.log(chalk.redBright(err));
throw err;
}
// Development console
if (dev) {
console.log(
chalk.green(
`> Development ${appName} Ready on ${chalk.yellowBright(
`http://localhost:${port}`
)} => ${chalk.cyanBright('HAPPY CODING :D')}`
)
);
(async () => {
// Opens the URL in the default browser.
await open(`http://localhost:${port}`);
})();
}
// Production console
else {
console.log(chalk.greenBright(`> ${appName} is already running`));
}
});
});