-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (82 loc) · 2.13 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Module dependencies.
*/
const express = require("express");
const compression = require("compression");
const bodyParser = require("body-parser");
const logger = require("morgan");
const chalk = require("chalk");
const errorHandler = require("errorhandler");
const dotenv = require("dotenv");
const path = require("path");
const mongoose = require("mongoose");
const expressValidator = require("express-validator");
const expressStatusMonitor = require("express-status-monitor");
const sass = require("node-sass-middleware");
const cors = require("cors");
/**
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.load({ path: ".env" });
/**
* Controllers (route handlers).
*/
const apiController = require("./controllers/api");
/**
* Create Express server.
*/
const app = express();
/**
* Connect to MongoDB.
*/
mongoose.Promise = global.Promise;
mongoose.connect(
process.env.MONGODB_URI || "mongodb://localhost:27017/vlctechhub",
{
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASS
}
);
mongoose.connection.on("error", err => {
console.error(err);
console.log(
"%s MongoDB connection error. Please make sure MongoDB is running.",
chalk.red("✗")
);
process.exit();
});
/**
* Express configuration.
*/
app.set("host", process.env.OPENSHIFT_NODEJS_IP || "0.0.0.0");
app.set("port", process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.use(cors());
app.use(expressStatusMonitor());
app.use(compression());
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
/**
* API routes.
*/
app.post("/user/:type", apiController.postUser);
app.put("/user/:type", apiController.updateUser);
app.delete("/user/:type", apiController.removeUser);
/**
* Error Handler.
*/
app.use(errorHandler());
/**
* Start Express server.
*/
app.listen(app.get("port"), () => {
console.log(
"%s App is running at http://localhost:%d in %s mode",
chalk.green("✓"),
app.get("port"),
app.get("env")
);
console.log(" Press CTRL-C to stop\n");
});
module.exports = app;