This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (45 loc) · 1.49 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
49
50
51
52
53
54
55
56
57
import express from "express";
import bodyParser from "body-parser";
import * as url from "url";
import {scrapePage} from "./scrapePage.cjs";
import axios from 'axios';
import fetch from 'node-fetch';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const app = express();
app.use(express.json({ limit: "50mb" }));
app.use(express.static(__dirname + "/src/"));
const jsonParser = bodyParser.json({ limit: "50mb" });
app.get("/", (req, res) => {
res.sendFile("index.html");
});
app.post("/api/process", (req, res) => {
if(!isValidHttpUrl(req.body.searchUrl))
{
res.send({msg: "Wrong! Incorrect url. Check it and try again"})
return;
}
let scrapeStuff = scrapePage(req.body.searchUrl);
scrapeStuff.then(async function (result) {
// Make request to our python API
const response = await fetch('http://127.0.0.1:5000/classify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ "text": result[0] })
});
const apiresponse = await response.json();
res.send({msg: apiresponse.prediction})
});
});
app.listen(3000, () => {
console.log("Server running o port 3000..");
});
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}