-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (66 loc) · 2.64 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
82
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const logger = require('morgan');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
const axios = require('axios');
const cheerio = require('cheerio');
const db = require('./models');
const app = express();
const PORT = process.env.PORT || 8080;
// Use Morgan Logger for Logging Requests
app.use(logger('dev'));
// Use Body-Parse for handling form submissions.
app.use(bodyParser.urlencoded({extended: true}));
// Use Express.static to serve up the public folder as a static directory.
app.use(express.static(__dirname + '/public'));
// Establishing Handlebars as the Templating Engine
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
require('./routes/htmlRoutes')(app);
require('./routes/apiRoutes')(app);
// Connect to the MongoDB Database
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost/week18test';
mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true});
// Routes --------------------------------------------------------------------------------------------------------------
/*app.get('/scrape', (req, res) => {
axios.get('https://lifehacker.com/tag/programming').then(response => {
console.log(response.data);
const $ = cheerio.load(response.data);
$('div.item__text').each(function (i, element) {
const result = {};
result.title = $(this).find('h1').text();
result.link = $(this).find('h1').children().attr('href');
result.author = $(this).find('div.author').text();
result.exerpt = $(this).find('div.excerpt').text();
db.Article.create(result)
.then(dbArticle => console.log(dbArticle))
.catch(err => res.json(err))
});
console.log('Scrape Complete');
res.send('Scrape Complete')
})
});
app.get('/articles', (req, res) => {
db.Article.find({})
.then(dbArticle => {
console.log(dbArticle);
res.render('index', {articles: dbArticle})})
.catch(err => res.json(err))
});
app.get('/articles/:id', (req, res) => {
db.Article.findOne({_id: req.params.id})
.populate('note')
.then(dbArticle => res.json(dbArticle))
.catch(err => res.json(err))
});
app.post('/articles/:id', (req, res) => {
db.Note.create(req.body)
.then(dbNote => db.Article.findOneAndUpdate({_id: req.params.id}, {note: dbNote._id}, {new: true}))
.then(dbArticle => res.json(dbArticle))
.catch(err => res.json(err))
});*/
// Start the Server
app.listen(PORT, () => console.log(`App running at http://localhost:${PORT}`));
module.exports = app;