-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
74 lines (66 loc) · 2.42 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
/*
* Copyright 2015 CareerBuilder, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
//
var express = require('express'); // call express
var https = require('https');
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
global.config = require('./config.json');
var app = express(); // define our app using express
app.set('view engine','html');
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname, 'client')));
app.set('views', __dirname + '/client/views');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
if(!global.config.CookieSecret){
var sec = require('crypto').randomBytes(32).toString('hex');
global.config.CookieSecret = sec;
fs.writeFileSync('./config.json', JSON.stringify(global.config, null, '\t'));
}
app.use(cookieParser(global.config.CookieSecret));
var port = 3000;
// ROUTES FOR OUR API
// =============================================================================
//
app.get('/', function(req, res){
res.render('index');
});
app.use('/api/', require('./routes/api.js'));
//keep this last, as it will return 404
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
return res.render('404', { url: req.url });
}
// respond with json
if (req.accepts('json')) {
return res.send({Error: 'Not a valid endpoint'});
}
// default to plain-text. send()
return res.type('txt').send('Not found');
});
if('SSL' in global.config){
var config = {
key: fs.readFileSync(global.config.SSL.keyfile),
cert: fs.readFileSync(global.config.SSL.certfile)
};
https.createServer(config, app).listen(port);
}
else{
app.listen(port);
}
console.log('Magic happens on port ' + port);