-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (52 loc) · 1.77 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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
const request = require("request");
const fetch = require("isomorphic-fetch");
const jsdom = require("jsdom");
const ejs = require("ejs");
const {recipeParser} = require('./scripts/recipeParser');
const { JSDOM } = jsdom;
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
//global variable declarations (needed to pass these as EJS variables to render "home")
let newList = [];
let oldList = [];
let servingsOld;
let servingsNew;
let url;
app.get('/', function (req, res) {
res.render('home', { oldList: oldList, newList: newList, servingsOld: servingsOld, servingsNew: servingsNew, url: url });
});
//this captures the user's HTML url & serving # form submission
app.post('/', function (req, res) {
url = req.body.url;
if (url.startsWith('http') == false) {
url = 'http://' + url;
}
servingsOld = req.body.servingsOld;
servingsNew = req.body.servingsNew;
const servingsMult = servingsNew / servingsOld;
//this function fetches the URL and uses the JSDOM library to get the dom
(async () => {
const response = await fetch(url);
const text = await response.text();
const dom = await new JSDOM(text);
//this requires jquery and loads the $ selector for use
const $ = (jQuery = require('jquery')(dom.window));
$(dom.window.document).ready(function () {
({oldList, newList} = recipeParser(dom, servingsMult));
res.redirect('/');
});
})();
});
let port = process.env.PORT;
if (port == null || port == '') {
port = 3000;
}
app.listen(port, () => {
console.log('Server is listening on port ' + port);
});