-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.89 KB
/
index.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
import chalk from "chalk";
import app from "@";
import { PORT, NODE_ENV } from "@/config/constants";
startServer();
function startServer() {
return new Promise((resolve, reject) => {
const httpServer = app.listen(PORT);
httpServer.once("error", err => {
if (err.code === "EADDRINUSE") {
reject(err);
}
});
httpServer.once("listening", () => resolve(httpServer));
})
.then(httpServer => {
const { port } = httpServer.address();
const running = chalk.hex("#10a567").bold("[RUNNING]");
const reloaded = chalk.hex("#007acc").bold("[RELOADED]");
const env = chalk.hex("#10a567").bold("[MODE]");
const url = chalk.bold(`http://localhost:${port}/api/status`);
console.info(`${running} 🌎 Listening on ${port}. Open up "${url}" in your browser.`);
// Hot Module Replacement API
if (module.hot) {
let currentApp = app;
module.hot.accept("./src", () => {
httpServer.removeListener("request", currentApp);
import("./src")
.then(({ default: nextApp }) => {
currentApp = nextApp;
httpServer.on("request", currentApp);
console.log("-------------------------");
console.log(`${env} ${NODE_ENV.toUpperCase()}`);
console.info(`${reloaded} Server reloaded! ${getTime()}`);
})
.catch(err => console.error(err));
});
// For reload main module (self). It will be restart http-server.
module.hot.accept(err => console.error(err));
module.hot.dispose(() => {
console.log("#### Disposing entry module...");
httpServer.close();
});
}
})
.catch(err => {
console.error(err);
});
}
function getTime() {
const date = new Date();
return `⏰ ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`;
}