generated from comp426-2022-spring/a04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (77 loc) · 2.9 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { exit } = require('process')
const express = require('express')
const app = express()
const morgan = require('morgan')
const fs = require('fs')
const args = require('minimist')(process.argv.slice(2), {
boolean: ['debug', 'log'],
default: {
debug: false,
log: false,
}
})
args['port', 'log', 'debug']
console.log(args)
if (args.help) {
console.log(
"server.js [options] \n" +
"\n" +
"\t --port \t Set the port number for the server to listen on. must be an integer between 1 and 65535.\n" +
"\t --debug \t If set to `true`, creates endpoints /app/log/access/ which returns a JSON access log from the database and /app/error/ which throws an error with the message " +
"\"Error test successful.\" " + "Defaults to `false`. \n" +
"\t --log \t If set to false, no log files are written. Defaults to 'true'. Logs are always written to database. \n" +
"\t --help \t Return this message and exit."
)
exit(0);
}
const database = require('./database')
app.use(express.urlencoded({extended: true}))
app.use(express.json())
// Start an app server
const port = args.port || 5555
const server = app.listen(port, () => {
console.log('App listening on port %PORT%'.replace('%PORT%', port))
});
app.get('/app/', (req, res) => {
// Respond with status 200
res.statusCode = 200;
// Respind with status message 'OK'
res.statusMessage = "OK";
res.writeHead( res.statusCode, { 'Content-Type' : 'text/plain '});
res.end(res.statusCode + ' ' + res.statusMessage);
});
// Middleware function
app.use( (req, res, next) => {
let logdata = {
remoteaddr: req.ip,
remoteuser: req.user,
time: Date.now(),
method: req.method,
url: req.url,
protocol: req.protocol,
httpversion: req.httpVersion,
status: res.statusCode,
referer: req.headers['referer'],
useragent: req.headers['user-agent']
}
const stmt = database.prepare(`INSERT INTO accesslog (remoteaddr, remoteuser, time, method, url, protocol, httpversion, status, referer, useragent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
stmt.run(logdata.remoteaddr, logdata.remoteuser, logdata.time, logdata.method, logdata.url, logdata.protocol, logdata.httpversion, logdata.status, logdata.referer, logdata.useragent)
next();
})
if (args.debug == true) {
app.get('/app/log/access', (req, res) => {
const select = database.prepare('SELECT * FROM accesslog').all();
res.status(200).json(select);
})
app.get('/app/error', (req, res) => {
throw new Error('Error test successful.')
})
}
if (args.log == true) {
const WRITESTREAM = fs.createWriteStream('FILE', { flags: 'a' });
app.use(morgan('FORMAT', { stream: WRITESTREAM }));
}
// Default response for any other request
app.use(function(req, res) {
res.status(404).send('404 NOT FOUND')
});