-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (56 loc) · 2.28 KB
/
server.js
File metadata and controls
71 lines (56 loc) · 2.28 KB
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
/* Showing Mongoose's "Populated" Method
* =============================================== */
var express = require('express');
var exphbs = require('express-handlebars');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var logger = require('morgan'); // for debugging
var request = require('request'); // for web-scraping
var cheerio = require('cheerio'); // for web-scraping
// Initialize Express for debugging & body parsing
var app = express();
app.use(logger('dev'));
app.use(bodyParser.urlencoded({
extended: false
}))
// Serve Static Content
app.use(express.static(process.cwd() + '/public'));
// Express-Handlebars
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// Database Configuration with Mongoose
// ---------------------------------------------------------------------------------------------------------------
// Connect to localhost if not a production environment
if(process.env.NODE_ENV == 'production'){
mongoose.connect('mongodb://heroku_58c6l923:2gecc0p46jq110h6lke7elc7vb@ds115214.mlab.com:15214/heroku_58c6l923');
}
else{
mongoose.connect('mongodb://localhost/WebScraper');
// YOU CAN IGNORE THE CONNECTION URL BELOW (LINE 41) THAT WAS JUST FOR DELETING STUFF ON A RE-DEPLOYMENT
//mongoose.connect('mongodb://heroku_58c6l923:2gecc0p46jq110h6lke7elc7vb@ds115214.mlab.com:15214/heroku_58c6l923');
}
var db = mongoose.connection;
// Show any Mongoose errors
db.on('error', function(err) {
console.log('Mongoose Error: ', err);
});
// Once logged in to the db through mongoose, log a success message
db.once('open', function() {
console.log('Mongoose connection successful.');
});
// Import the Comment and Article models
var Comment = require('./models/Note.js');
var Article = require('./models/Article.js');
// ---------------------------------------------------------------------------------------------------------------
// DROP DATABASE (FOR MY PERSONAL REFERENCE ONLY - YOU CAN IGNORE)
// Article.remove({}, function(err) {
// console.log('collection removed')
// });
// Import Routes/Controller
var router = require('./controllers/controller.js');
app.use('/', router);
// Launch App
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log('Running on port: ' + port);
});