forked from slemke/open-geo-tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (42 loc) · 1.32 KB
/
server.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
const express = require('express')
const https = require('https');
const app = express()
const config = require('./config.js');
const bodyParser = require('body-parser');
const fs = require('fs')
const mongoose = require('mongoose');
//connect to MongoDB
mongoose.connect(config.mongodb, { useMongoClient: true } );
const db = mongoose.connection;
//handle mongo error
db.on('error', console.error.bind(console, 'connection error:'));
// use json bodyparser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// setup routing for assets
app.use('/static', express.static(__dirname + '/assets'));
// use modules
app.use(require('./modules'));
// set base file for frontend output
app.get('/', function (req, res) {
res.sendFile(__dirname + '/assets/app/index.html');
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
// setup server
const privateKey = fs.readFileSync( 'key.pem' );
const certificate = fs.readFileSync( 'cert.pem' );
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(config.port);