forked from inidaname/localHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (63 loc) · 2.34 KB
/
app.js
File metadata and controls
84 lines (63 loc) · 2.34 KB
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
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const path = require('path');
const hbs = require('handlebars');
const bodyParser = require('body-parser');
const routes = require('./routes/index')
const expressValidator = require('express-validator');
const errorHandlers = require("./lib/handleError");
const app = express();
// import environmental variables from our variables.env file
require("dotenv").config({ path: "./keys.env" });
// Connect to our Database and handle an bad connections
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true });
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on('error', (err) => {
console.error(`${err.message}`);
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(bodyParser.urlencoded({ extended: true }));
// Exposes a bunch of methods for validating data. Used heavily on userController.validateRegister
app.use(expressValidator());
//handling CORS
app.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"
);
if(req.method === 'OPTIONS'){
res.header("Access-Control-Allow-Methods","POST, PUT, GET, DELETE, PATCH");
return res.status(200).json({});
}
next();
});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index', {
title: 'Home'
});
})
app.get('/user', function(req, res) {
res.render('user', {
title: 'User Section'
});
});
app.use('/api', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
app.set('PORT', process.env.PORT);
const server = app.listen(app.get('PORT'), function() {
console.log(`MLH app running port ${app.get('PORT')}`);
});