forked from comp426-2022-spring/a99-izar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (68 loc) · 2.04 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import express from 'express'
import minimist from 'minimist'
import { getAccessDb } from './src/logdatabase.js'
import getSentiment from './src/twitter.cjs'
const app = express()
const args = minimist(process.argv.slice(2))
const db = getAccessDb()
console.log(args)
const HTTP_PORT = (1 <= args.port && args.port <= 65535) ? args.port : 5000
const server = app.listen(HTTP_PORT, () => {
console.log('App listening on port %PORT%'.replace('%PORT%', HTTP_PORT))
})
const logging = (req, res, next) => {
const query = db.prepare(`
INSERT INTO accesslog (
ip,
userid,
time,
method,
url,
protocol,
httpversion,
status,
referer,
userinfo
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
)
const json = query.run(req.ip, req.user, Date.now(), req.method, req.url, req.httpVersion,
req.protocol, req.statusCode, req.headers['referers'], req.headers['user-agent']
)
next()
}
app.use(express.json())
//app.use(logging)
app.use(express.static('./dist'))
app.get('/app/', (req, res) => {
// Respond with status 200
res.statusCode = 200;
// Respond with status message "OK"
res.statusMessage = 'OK';
res.writeHead( res.statusCode, { 'Content-Type' : 'text/plain' });
res.end(res.statusCode+ ' ' +res.statusMessage)
});
if(args.debug) {
app.get('/app/log/access/', (req, res, next) => {
try {
const query = db.prepare('SELECT * from accesslog').all()
res.status(200).json(query)
} catch {
console.error(e)
}
})
}
app.get('/app/sentiment/:state', (req, res, next) => {
getSentiment(req, res, next);
});
app.use(function (req, res, next) {
res.statusCode = 404;
// Respond with status message "OK"
res.statusMessage = 'NOT FOUND';
res.writeHead( res.statusCode, { 'Content-Type' : 'text/plain' });
res.end(res.statusCode+ ' ' +res.statusMessage)
})
export var globalVariable = {
state : "South Carolina"
};