-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (42 loc) · 1.55 KB
/
index.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
45
46
47
48
const port = process.env.PORT || 4000;
const host = process.env.HOST || '127.0.0.1';
const express = require('express');
const app = express();
const logger = require('./utils/logger');
const scraper = require('./utils/scraper');
const findPublishers = require('./utils/findPublishers');
const handleArticle = require('./utils/handleArticle');
app.listen(port, host);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.json()); // support json encoded bodies
app.use(express.urlencoded({extended: true})); // support encoded bodies
// set up the logger
logger(app);
app.post('/linkArticle', (req, res) => {
let url = req.body.url;
const cdbid = req.body.cdbid;
scraper(url, res)
.then( response => {
const meta = response; // get the metadata
const foundPublishers = findPublishers(meta.url); // check if publisher is trusted
if(foundPublishers.length) {
// trusted publisher
const foundPublisher = foundPublishers[0];
meta.publisher = foundPublisher.name;
// handle article
handleArticle(res, cdbid, meta);
} else {
// unknown publisher
res.message = `No trusted publisher is found for ${meta.url}`;
res.status(200).send(res.message);
}
})
.catch( err => {
res.message = `Impossible to scrape url: ${err}`;
res.status(200).send(res.message);
});
});