-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
executable file
·70 lines (59 loc) · 2.33 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require("express"),
app = express(),
ejs = require("ejs"),
keys = require("./config/keys.js"),
bodyParser = require("body-parser"),
Sentiment = require('sentiment'),
sentiment = new Sentiment(),
html = require('html-parse-stringify'),
stringToDom = require('string-to-dom');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
app.set("view engine", "ejs"); // Rendering engine defined as EJS
app.use(express.static(__dirname + '/public')); // Tells express, CSS is in public folder
app.set("views", "views"); // Tells EJS the path to the "views" directory
app.use(bodyParser.urlencoded({extended: true})); // bodyParser config
// const sentiment = new Sentiment(); // Set's up thing for sentiment
// Index Route, redirects to display homepage
app.get("/", (req, res, next) => {
res.redirect("index");
});
// Renders the index page
app.get("/index", (req, res, next) => {
res.render("index");
})
// Post route, the forms sends a URL to be exported as an object with article feautures
app.post("/index", (req, res, next) => {
// Initializes article-parser, which helps parse articles into object forme
const {
extract
} = require('article-parser');
// User-entered URL
let url = req.body.url;
extract(url).then((article) => {
const articleInHTMLForm = article.content;
const articleInTextForm = articleInHTMLForm
.replace(/<\/?[^>]+(>|$)/g, " ") //Replaces the Tags and leaves a space.
.replace(/ +/g, " ") //Replaces double spaces and leaves a single.
.replace(/ \.+/g, "."); //Replaces the space between a word and the period to end a sentence.
//title, publishedTime, author, source, content, url,
//Formatts all of the neccesary inforamtion into one object
const articleFormatting = {
title: article.title,
publishedTime: article.publishedTime,
author: article.author,
source: article.source,
content: articleInTextForm,
url: article.url
};
return articleFormatting;
}).then((article) => {
res.render("new", { article: article, Sentiment: Sentiment, html: html, stringToDom: stringToDom, JSDOM: JSDOM}); //Must be an object
}).catch((err) => {
console.log(err);
});
});
// Server Setup/Initialization
app.listen(process.env.PORT || keys.PORT, () => {
console.log(`Server running on port ${keys.PORT}!`);
});