-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.es6
executable file
·76 lines (68 loc) · 2.1 KB
/
server.es6
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
#!/usr/bin/env node
/* eslint no-debugger: "warn" */
// import net from 'net';
import http from 'http';
import qs from 'querystring';
import parseArgs from 'minimist';
import lookup from './scrabble-lookup';
const defaultPort = 3456;
const usage = `usage: ${process.argv[1].replace(/.+\//, '')} [-h] [-p port]
where: -p -- server port (default ${defaultPort})
`;
const requestHandler = (request, response) => {
console.log(`connection ${request.method}`);
if (request.method !== 'POST') {
response.writeHead(405, 'Method Not Supported', { 'Content-Type': 'text/plain' });
response.end('405: Method Not Supported');
return;
}
let msgBody = '';
request.on('data', (data) => {
msgBody = msgBody.concat(data.toString());
if (msgBody.length > 100) {
response.writeHead(413, 'Request Entity Too Large', { 'Content-Type': 'text/plain' });
response.end('413: Request Entity Too Large');
}
});
request.on('end', () => {
const input = qs.unescape(msgBody);
console.log(`word: ${input}`);
const words = lookup(input);
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end(JSON.stringify(words));
});
};
const startHttpServer = (port) => {
const svr = http.createServer(requestHandler);
svr.on('error', (e) => { throw e; });
svr.listen(port, () => console.log(`server listening on port ${port}`));
};
/*
const startTcpServer = (port) => {
const svr = net.createServer((conn) => {
console.log('client connected');
conn.on('request', (data) => {
const input = scrabbleWords(data.toString().trim());
console.log(`word: ${input}`);
const words = scrabbleWords(input);
conn.end(JSON.stringify(words));
});
});
svr.on('error', (e) => { throw e; });
svr.listen(port, () => console.log(`server listening on port ${port}`));
};
*/
const main = () => {
const args = parseArgs(process.argv.slice(2), {
boolean: ['h'],
string: ['p'],
});
debugger;
if (args.h) {
console.log(usage);
process.exit(0);
}
// startTcpServer(args.p || defaultPort);
startHttpServer(args.p || defaultPort);
};
main();