-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (71 loc) · 2.18 KB
/
server.js
File metadata and controls
89 lines (71 loc) · 2.18 KB
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
process.title = 'mailsac_spam_api'
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const Classifier = require('./lib/classifier')
const formatEmail = require('./lib/formatEmail')
const modelPath = process.env.MODEL
const port = process.env.PORT || '3000'
const debug = console.log
if (!modelPath) {
debug('Missing environment variable MODEL with path to a trained JSON model')
process.exit(1)
}
const spamminessDecimalPlaces = 20 // to avoid scientific notation
const classifier = new Classifier()
classifier.load(modelPath)
debug('Loaded classifier from', modelPath)
const app = express()
app.use(morgan('tiny'))
app.disable('x-powered-by')
// parse application/json
app.use(bodyParser.json())
app.post('/api/spam-checks', function postSpamCheck (req, res) {
const { to, from, subject, text } = req.body
if (!to || !from || !subject || !text) {
res.status(400)
.send({
url: req.url,
message: 'The following JSON fields are required: to, from, subject, text'
})
return
}
const formattedEmail = formatEmail({ to, from, subject, text })
const spamminess = classifier.predict(formattedEmail)
.toFixed(spamminessDecimalPlaces)
debug('prediction', spamminess, { to, from, subject })
// avoid scientific notation in JSON for readability
// express will parse it, if it knows it is application/json
const body = `{
"spamminess": ${spamminess},
"spam": ${spamminess > classifier.pValueSpamMinimum}
}`
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': body.length,
// RCF-1123
'Date': new Date().toUTCString()
})
res.end(body)
})
app.use(function handle404 (req, res) {
res.status(404)
.send({ url: req.url, message: 'Route not matched' })
})
app.use(function lastErrorHandler (err, req, res, next) {
debug('error', err.message, err.stack, err.code)
res.status(500)
.send({
url: req.url,
message: err.message,
stack: err.stack,
code: err.code
})
})
app.on('error', (err) => {
debug('failed starting up', err)
process.exit(1)
})
app.listen(port, () => {
debug('spam server is listening on port', port)
})