<file_sep>// Dependencies
var express = require("express");
var mongoose = require("mongoose");
var exphbs = require("express-handlebars");
var db = require("./models");
var path = require("path");
var app = express();
var axios = require("axios");
var cheerio = require("cheerio");

// Port Declaration
var PORT = process.env.PORT || 3000;

// Configure Middleware
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// Handlebars
app.engine(
    "handlebars",
    exphbs({
        defaultLayout: "main"
    })
);
app.set("view engine", "handlebars");

// Connecting to MongoDB
// If deployed, use the deployed database. Otherwise use the local mongoHeadlines database
var MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/mongoHeadlines";
mongoose.connect(MONGODB_URI);

// Routes
// require("./routes/apiRoutes")(app);
require("./routes/htmlRoutes")(app);

// A GET route for scraping news articles
app.get("/scrape", function (req, res) {
    // First, we grab the body of the html with axios
    axios.get("http://www.gamesradar.com/news/").then(function (response) {
        var $ = cheerio.load(response.data);

        // Go through each article div tag and get articles based on the results.
        $("a.article-link").each(function (i, element) {
            var result = {};
            var validResults = {};

            result.link = $(this).attr("href");
            console.log("Link:", result.link);
            result.title = $(this).children("article").children("div.content").children("header").children("h3").text();
            console.log("Title:", result.title);
            var summary = $(this).children("article").children("div.content").children("p.synopsis").text();
            result.summary = summary.substr(summary.indexOf('\n') + 1);

            console.log("Summary:", result.summary);

            validResults.title = result.title;
            validResults.link = result.link;
            validResults.summary = result.summary;

            db.Article.create(validResults)
                .then(function (article) {
                    console.log(article);
                })
                .catch(function (err) {
                    console.log(err);
                });
        });
        res.send("Scrape complete");
    });
});

// Route for getting all articles from the db
app.get("/articles", function (req, res) {
    // Grab every document in the Articles Collection
    db.Article.find({})
        .then(function (article) {
            res.json(article);
        })
        .catch(function (err) {
            res.json(err);
        });
});

// Route for grabbing a specific Article by Id, populate it with it's note.
app.get("/articles/:id", function (req, res) {
    db.Article.findOne({ _id: req.params.id })
        .populate("note")
        .then(function (article) {
            res.json(article);
        })
        .catch(function (err) {
            res.json(err);
        });
});

// Route for saving/updating an Article's associated note
app.post("/articles/:id", function (req, res) {
    db.Note.create(req.body)
        .then(function (note) {
            return db.Article.findOneAndUpdate({ _id: req.params.id }, { note: note._id }, { new: true });
        })
        .then(function (article) {
            // If unable to update an Article, send it back to the client
            res.json(article);
        })
        .catch(function (err) {
            res.json(err);
        });
});

// Start the Server
app.listen(PORT, function () {
    console.log("App running on port " + PORT + "!");
});
<file_sep># NewScraper
Scrape news articles for personal perusal.

 * Headline - the title of the article

 * Summary - a short summary of the article

 * URL - the url to the original article

 * Feel free to add more content to your database (photos, bylines, and so on).

Users should also be able to leave comments on the articles displayed and revisit them later. The comments should be saved to the database as well and associated with their articles. Users should also be able to delete comments left on articles. All stored comments should be visible to every user.



Beyond these requirements, be creative and have fun with this!

**Heroku deployment**: https://git.heroku.com/gentle-springs-30491.git
<file_sep>const db = require("../models");

module.exports = function (app) {
    // Load Index Page
    app.get("/", function (req, res) {
        db.Article.find({})
            .then(function (data) {
                res.render("index", {
                    article: data
                });
            });
    });
}<file_sep>

$(document).on("click", "#saveArticle", function () {
    event.preventDefault();
    var thisArticle = $(this)
    console.log("this article:", thisArticle);
});

$(document).on("click", "#navScrape", function () {
    event.preventDefault();
    $.get("/scrape", function (req, res) {
        console.log("Articles Scraped!");
    }).then(function () {
        location.reload();
    })
});

$(document).on("click", "#navClear", function () {
    event.preventDefault();
    $("#articles").empty();
});

$(document).on("click", "#addNote", function () {
    event.preventDefault();
    console.log("this:", $(this).attr("data"));
    var thisId = $(this).attr("data");

    $.ajax({
        method: "GET",
        url: "/articles/" + thisId
    })
        .then(function (data) {
            console.log("ajax data:", data);
            $("#notes").append("<h5>" + data.title.substring(0, 72) + '...' + "</h5>");
            $("#notes").append("<input id='titleinput' name='title' >");
            $("#notes").append("<textarea id='bodyinput' name='body'></textarea>");
            $("#notes").append("<button class='btn-large blue waves-effect right' data-id='" + data._id + "' id='saveNote'>Save Note</button>");

            // If there's a note in the article
            if (data.note) {
                $("#titleinput").val(data.note.title);
                $("#bodyinput").val(data.note.body);
            }


        })
});

$(document).on("click", "#saveNote", function () {
    event.preventDefault();

    var thisId = $(this).attr("data-id");
    console.log("ThisId:", thisId);

    $.ajax({
        method: "POST",
        url: "/articles/" + thisId,
        data: {
            title: $("#titleinput").val(),
            body: $("#bodyinput").val()
        }
    })
        .then(function (data) {
            console.log("last data:", data);
        });
    $("#notes").empty();

    // Remove values entered
    $("#titleinput").val("");
    $("#bodyinput").val("");
});