-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (45 loc) · 1.56 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
53
54
55
56
57
58
59
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var api = require('./routes/api');
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var admin_user = process.env.ADMIN_USER || 'admin',
admin_pwd = process.env.ADMIN_PASS || 'mypasswordsucks';
var app = express();
// Authenticator
app.use(function(req, res, next) {
var auth;
if (req.headers.authorization) {
auth = new Buffer(req.headers.authorization.substring(6), 'base64').toString().split(':');
}
if (!auth || auth[0] !== admin_user || auth[1] !== admin_pwd) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
res.end('Unauthorized');
} else {
next();
}
});
// no caching
app.use(function (req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next()
});
//View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// Set Static Folder
app.use(express.static(path.join(__dirname,'static')));
// Body Parser MW
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', index);
app.use('/api', api);
app.listen(port, ip);
console.log('Server running on http://%s:%s', ip, port);
module.exports = app;