forked from cakrayp/ssweb-api-caliph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
48 lines (43 loc) · 1.49 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
const express = require('express');
const app = express();
const port = process.env.PORT || 8050;
const requestIp = require('request-ip');
const momentjs = require('moment-timezone');
const { author } = require('./index');
const helpandUsage = require('./src/readme.min');
const Controller = require('./src/controllers');
const { color } = require('./lib/color');
// Express Controller.
app.use(express.static("public"))
app.enable('trust proxy');
app.set("json spaces", 4);
app.use(requestIp.mw());
// Express logs.
app.use(async(req, res, next) => {
if (process.env.ENABLE_LOG_EXPRESS) {
const isApi = /\/api\/webscreen/.test(req.path);
const hostDoamin = req.hostname==='localhost'?'127.0.0.1':req.hostname;
console.log(
color(`${momentjs.tz("Asia/Jakarta").toISOString()} Express-Action[${isApi?'api':'router'}]:`, "magenta"),
color(req.protocol.toUpperCase(), "yellow"),
req.method,
color(`"${req.originalUrl}"`),
color(`host=${hostDoamin}`, "blue"),
`fwd=${req.clientIp}`
);
};
next();
});
// Route Action.
app.get('/', async (req, res) => {
res.setHeader('Content-type', 'text/plain').send(helpandUsage(req));
});
app.get('/api/webscreen', Controller.webScreenschot);
app.use(async (req, res) => {
res.setHeader('Content-type', 'text/plain');
res.status(404).send("404 Not found");
});
// Listen Port.
app.listen(port, () => {
console.log('Server is running on port : ' + port);
});