-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (43 loc) · 1.63 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
// ~~~~~~~~~~~~MODULES~~~~~~~~~~~~~~~~~ //
var express = require('express');
var morgan = require('morgan');
var mongoose = require('mongoose');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var loadUser = require('./middleware/loadUser');
// require('dotenv').load();
// Instance of Express
var app = express();
// Old Connect Mongoose to MongoDB
var mongoPath = process.env.MONGOLAB_URI || 'mongodb://localhost/hearsays';
// Working on heroku
// Connect Mongoose to MongoDB
// var mongoPath = 'mongodb://heroku_1qwbzvwc:[email protected]:59185/heroku_1qwbzvwc';
//herku can't find the process.env so we had to take the hardwired url and insert it here...now it breaks my local
mongoose.connect(mongoPath);
// Setup EJS engine support
app.set('view engine', 'ejs');
// ~~~~~~~~~~MIDDLEWARE~~~~~~~~~~~~~ //
app.use(express.static('./public'));
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended: true}));
app.use(loadUser);
// Custom Middleware for Accessing APIs
app.use(function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
next();
});
// ~~~~~~~~~~IMPORT ROUTER~~~~~~~~~~~~~ //
var indexRouter = require('./routes/index');
var hearsaysRouter = require('./routes/api/hearsays');
var usersRouter = require('./routes/users');
// ~~~~~~~~~~MAP ROUTER~~~~~~~~~~~~~ //
app.use('/', indexRouter);
app.use('/api/hearsays', hearsaysRouter);
app.use('/api/users', usersRouter);
// ~~~~~~~~~~LISTENER~~~~~~~~~~~~~ //
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log('Listening on '+ port);
});