-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
66 lines (58 loc) · 2.14 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
/*
* Here is where we import all of our dependancies. The first line imports
* dotenv and the loads all of the environment varibles specified in there.
* The next three lines are importing node modules that we installed with npm.
*
* Express acts as our webserver. It helps us render HTML to the user.
* Express-handlebars is our templating engine. We don't want static HTML, but
* rather we want to render handlebar-markup into HTML. This lets us pass
* variables have true control flow in our views.
* Mongoose is how we interface with our database, MongoDB.
*/
require('dotenv').load();
var express = require('express');
var exphbs = require('express-handlebars');
var mongoose = require('mongoose');
/*
* Connect to MongoDB with the database specified in our .env file.
*/
mongoose.connect(process.env.MONGODB);
/*
* Create an instance of our express webserver, and set the port.
*/
var app = express();
app.set('port', (process.env.PORT || 8080));
/*
* Tell express which view engine we're using, and tell handlebars where the
* main layout (the part that is rendered for every page) is.
*/
app.set('views', 'src/views')
app.engine('handlebars', exphbs({
layoutsDir: 'src/views/layouts/',
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
/*
* Bind our static assets to public endpoints.
*/
app.use('/assets', express.static(__dirname + '/src/assets'));
app.use('/vendor', express.static(__dirname + '/bower_components'));
/*
* Import our home and post controllers.
*/
var homeController = require('./src/controllers/home');
var postController = require('./src/controllers/post');
/*
* When a user makes a GET request to the root of our web app call the getHome
* function in our homeController. And when a user makes a GET request to any
* endpoint following the syntax "/<anything>" pull out the part after the "/"
* (:postURL) and call the function getPost in the postController.
*/
app.get('/', homeController.getHome);
app.get('/:postURL', postController.getPost);
/*
* Start the server!
*/
var server = app.listen(app.get('port'), function () {
console.log('the server is listening on port %s', app.get('port'));
});