-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
29 lines (22 loc) · 740 Bytes
/
api.js
File metadata and controls
29 lines (22 loc) · 740 Bytes
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
import express from 'express';
import crawl from './routes/crawl';
import bodyParser from 'body-parser';
const depromise = route => (req, res) => {
route(req, res)
.then((response) => res.json(response))
// TODO: switch to `.catch(({ status, message } = error) => {` when Babel 6 is fixed
.catch((error) => {
let { status, message } = error;
if(isNaN(status)) {
console.log("EXCEPTION!", error, error.stack);
status = 500;
message = 'oops, server error';
}
return res.status(status).json({ error: { message } });
});
};
const api = express();
api.use(express.static('app'));
api.use(bodyParser.json());
api.post('/crawl', depromise((req) => crawl(req.body.crawlUrl)));
export default api;