-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (44 loc) · 1.5 KB
/
app.js
File metadata and controls
53 lines (44 loc) · 1.5 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
'use strict';
var express = require('express'),
engines = require('consolidate'),
path = require('path'),
routes = require('./routes'),
mongo = require('mongodb').MongoClient,
logger = require('morgan'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser');
var password = process.argv.slice(2)[0];
if(!password) throw new Error("You need to specify the database password (npm start -- <password>)");
var url = "mongodb://inquire:"+password+"@cluster0-shard-00-00-vdkgp.mongodb.net:27017"+
",cluster0-shard-00-01-vdkgp.mongodb.net:27017,cluster0-shard-00-02-vdkgp.mongodb.net:27017"+
"/inquire?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"
mongo.connect(url, function(error, database) {
if(!error) {
initApp(database.db('inquire'));
}
else {
console.log("Error: " + error);
}
});
function initApp (db) {
var app = express();
// Set DB
app.set('db', db);
// Home and port
app.set('home', __dirname);
app.set('port', process.env.PORT || 3000);
// Rendering engine - HTML with hogan.js
app.engine('html', engines.hogan);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
// Middleware for logs
app.use(logger('dev'));
// Middleware to support cookies
app.use(cookieParser());
// Middleware to handle POST req
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
routes(app);
app.listen('3000');
console.log('Listening on port 3000');
}