This repository has been archived by the owner on Dec 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (46 loc) · 2 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
57
58
59
60
61
62
63
64
65
66
67
//TODO: clean up comments!
//TODO: fix bodyparser stuff!
//==== Require Things! ==========================================
// Misc Stuff
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var mongoose = require('mongoose');
// Get config data for mongoDB.
var mongoConfig = require('./config/mongo.js');
//==== Use pets! ==========================================================
// Including body-parser. See readme for info.
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// make the "public" directory available to the client
app.use(express.static('client'));
//==== Connect to the Database ===============================================
// Connect to Mongo server (hosted)
//mongoose.connect(mongoConfig.mongo.host, mongoConfig.mongo.db, mongoConfig.mongo.port, mongoConfig.mongo.options);
// Connect to local Mongo Database. This should work on the lab machines, if it doesn't we need to investigate...
mongoose.connect('mongodb://127.0.0.1:27017/pets');
//==== Misc ===================================================================
// define options that Express will use when sending files to the client (using res.sendFile)
var options = {
root: "client/",
dotfiles: 'deny'
};
//===== Defining Routes ========================================================
// API Routes
app.use('/api/pets', require('./api/pets'));
// Non-API routes
app.get('/*', function(req, res){
res.sendFile('index.html', options);
});
// Wildcard catches requests for non-existing routes or files and responds with a 404 html page
app.get('*', function(req, res){
//res.send("404");
res.sendFile('views/404/404.html', options);
});
//==== Starting the server =====================================================
// Starts the nodejs server on port 9000
var server = app.listen(9000, function(){
var host = server.address().address;
var port = server.address().port;
console.log("server listening on http://%s:%s", host, port);
});