-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (38 loc) · 1.12 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
const http = require('http')
const bankai = require('bankai/http')
const SpellChecker = require('spellchecker')
const compiler = bankai(__dirname)
const server = http.createServer(function (req, res) {
if (req.url === '/spellcheck') {
let text = ''
req.on('data', function (chunk) {
text += chunk
})
req.on('end', function () {
try {
const response = {status: 'ok', suggestions: []}
const word = text.toString()
if (SpellChecker.isMisspelled(word)) {
response.status = 'misspelled'
response.suggestions = SpellChecker.getCorrectionsForMisspelling(text.toString())
}
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(response))
} catch (err) {
res.statusCode = 400
res.sendDate(err.message)
}
})
req.on('error', function (err) {
res.statusCode = 400
res.sendDate(err.message)
})
} else {
compiler(req, res, function () {
res.statusCode = 404
res.end('not found')
})
}
})
server.listen(process.env.PORT || 8080)