-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (61 loc) · 2.18 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
// Import dependencies
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require('mongoose');
// Define port to run api server on
var port = 81;
// Define the application and router objects.
var app = express();
var router = express.Router();
// Define the path to any web pages to serve
var path = __dirname + "/views/";
// Bring in Mongoose models
require("./department");
require("./template");
require("./user");
// Bring in Endpoints for objects
var departments = require("./departments");
var templates = require("./templates.js");
var users = require("./users.js");
// Define promise library for mongoose to use as default
mongoose.Promise = global.Promise;
// Connect to remote Mlab database
mongoose.connect("mongodb://admin:[email protected]:37947/screen-art", {
useMongoClient: true
}, function (error) {
console.log(error);
})
// Tell express to use the JSON Body parser middleware
app.use(bodyParser.json());
// Tell express to use static paths to the web pages
app.use(express.static(path));
// Define the router object as express middleware
app.use("/", router);
// Tell the router what endpoint to use for each overall endpoint file
app.use("/departments", departments);
app.use("/templates", templates);
app.use("/users", users);
// Define application CORS to allow standard CRUD operations
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS')
next()
})
// Define a basic endpoint to return the index.html, mainly used for getting the https cert.
router.get("/", (req,res) => {
res.sendFile(path + "index.html");
})
// Tell the express web server to start on the defined port and log a message to console
app.listen(port, () => {
console.log("Live at Port " + port);
})
// Establish logging where everytime the API is queried it is console logged
router.use( (req,res,next) => {
console.log("/" + req.method);
next();
})
// Define 404 page as fallback state
app.use("*", (req,res) => {
res.sendFile(path + "404.html");
})