-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (31 loc) · 1.01 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
const express = require('express');
const dotenv = require('dotenv');
const util = require('util');
const body_parser = require('body-parser');
const text_parser = body_parser.text();
dotenv.config();
const app = express();
const fetch = require('node-fetch');
app.listen(process.env.PORT, () => {
console.log("listening on port " + process.env.PORT);
});
app.get('/', (req, res) => {
res.sendFile('index.html', {root: __dirname})
})
app.use(express.static('public'));
app.use(express.json());
app.post('/', text_parser, (req, res) => {
// console.log("Request body: "+util.inspect(req.body, false, null));
fetch(`https://dev.to/api/articles?api-key=${process.env.APIKEY}&username=${req.body}&state=all`)
.then((response) => {
return response.json()
}).then((data) => {
if(data.length > 0){
res.send({blogs: data})
} else {
res.send({blogs: []})
}
}).catch((err) => {
console.error(err);
})
})