-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (29 loc) · 855 Bytes
/
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
import http from 'http';
import log from './lib/log';
import scrape from './lib/scrape';
import { parseQuery, parsePath } from './lib/uri';
const port = process.env.VCAP_APP_PORT || process.env.port || 3000;
const server = http.createServer();
const router = {
'/': function(req, res) {
const query = parseQuery(req.url);
scrape(query.url, query.selector, response => {
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify(response));
res.end();
});
}
};
const route = function(req, res) {
log(req);
const path = parsePath(req.url);
if (path in router) {
router[path](req, res);
} else {
res.write('Could not find route ' + req.url);
res.statusCode = 404;
res.end();
}
};
server.on('request', route);
server.listen(port, () => console.log('listening on port', port));