-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 1.89 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
//REQUIRE PACKAGES
pry = require('pryjs')
var express = require('express');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var mongoose = require('mongoose');
var session = require('express-session');
var moment = require('moment');
var logger = require('morgan');
var hbs = require('hbs');
//REQUIRE CONTROLLER FILES
var usersController = require('./controllers/users.js');
var sessionsController = require('./controllers/sessions.js');
var ticketsController = require('./controllers/tickets.js');
var listsController = require('./controllers/lists.js');
//START SERVER INSTANCE
var app = express();
//CONNECT DB WITH DB LOCATION EITHER SET BY HEROKU IF USING THE ONLINE VERSION OR SPECIFIED HERE IF USING LOCALHOST
var mongoURI = process.env.MONGODB_URI || 'mongodb://localhost/todo-tracker';
mongoose.connect(mongoURI);
// Now that we're connected, let's save that connection to the database in a variable.
var db = mongoose.connection;
// Will log an error if db can't connect to MongoDB
db.on('error', function(err){
console.log(err);
});
// Will log "database has been connected" if it successfully connects.
db.once('open', function(){
console.log('database connected!');
});
//CONFIGURE MIDDLEWARE/PACKAGES
app.set('view engine', 'hbs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(logger('dev'));
app.use(methodOverride('_method'));
app.use(session({
secret: "SuperDuperSecretPass",
resave: false,
saveUninitialized: false
}));
//SET PUBLIC FOLDER FOR LINKING CLIENT-SIDE FILES
app.use(express.static('public'));
//SET ROUTES TO HIT CONTROLLERS
app.use('/users', usersController);
app.use('/sessions', sessionsController);
app.use('/lists/tickets', ticketsController);
app.use('/', listsController);
//CONNECT SERVER TO WORLD :)
app.listen(process.env.PORT || 4000, function(){
console.log('Server listening on 4000!')
})