-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.js
31 lines (25 loc) · 895 Bytes
/
httpserver.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
#!/usr/bin/env node
'use strict';
const http = require('http');
const url = require('url');
const search = require('./search');
const process = require('process');
(async()=> {
const server = http.createServer( (request, response) => {
let queryData = url.parse(request.url, true).query;
let contentType = {"Content-Type":"application/json"};
if(queryData.keyword && queryData.url) {
search(queryData.keyword, queryData.url).then(v=>{
response.writeHead(200, contentType);
response.end(JSON.stringify(v, 4));
}).catch(v=>{
response.writeHead(400, contentType);
response.end(JSON.stringify(v));
})
} else {
response.writeHead(400, contentType);
response.end();
}
});
server.listen(process.env.port || 8000);
})();