-
Notifications
You must be signed in to change notification settings - Fork 91
/
server.ts
67 lines (54 loc) · 1.41 KB
/
server.ts
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 * as path from 'path';
import * as fs from 'fs';
import * as express from 'express';
const app = express()
import factory from './lib/Factory';
import { api } from './routes/api';
const setupServer = () => {
const SERVER_PORT = process.env.MONGOKU_SERVER_PORT || 3100;
app.get('/', (req, res, next) => {
res.sendFile("app/index.html", { root: __dirname }, (err) => {
if (err) {
return next(err);
}
});
});
app.use('/api', api);
app.get('/*', (req, res, next) => {
const ext = path.extname(req.url);
fs.stat(path.join(__dirname, "app", req.url), (err, stats) => {
let file = "index.html";
if (stats && stats.isFile()) {
file = req.url;
}
res.sendFile(file, { root: path.join(__dirname, "app") }, (err) => {
if (err) {
return next(err);
}
});
});
});
app.use((err: Error, req: express.Request, res: express.Response, next) => {
res.status(500);
return res.json({
ok: false,
message: err.message,
});
});
app.listen(SERVER_PORT, () => console.log(`[Mongoku] listening on port ${SERVER_PORT}`));
}
export const start = async () => {
console.log(`[Mongoku] Starting...`);
try {
await factory.load();
setupServer();
} catch (err) {
console.error(err);
process.exit(1);
}
};
if (require.main === module) {
(async () => {
await start();
})();
}