-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·137 lines (119 loc) · 4.14 KB
/
server.js
File metadata and controls
executable file
·137 lines (119 loc) · 4.14 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var path = require('path'),
express = require('express'),
browserSync = require('browser-sync'),
nunjucks = require('express-nunjucks'),
routes = require(__dirname + '/app/routes.js'),
favicon = require('serve-favicon'),
app = express(),
basicAuth = require('basic-auth'),
bodyParser = require('body-parser'),
config = require(__dirname + '/app/config.js'),
port = (process.env.PORT || config.port),
utils = require(__dirname + '/lib/utils.js'),
packageJson = require(__dirname + '/package.json'),
filters = require(__dirname + '/app/filters.js'),
_ = require('lodash'),
// Grab environment variables specified in Procfile or as Heroku config vars
releaseVersion = packageJson.version;
username = process.env.USERNAME,
password = process.env.PASSWORD,
env = process.env.NODE_ENV || 'development',
useAuth = process.env.USE_AUTH || config.useAuth;
env = env.toLowerCase();
useAuth = useAuth.toLowerCase();
// Authenticate against the environment-provided credentials, if running
// the app in production (Heroku, effectively)
if (env === 'production' && useAuth === 'true'){
app.use(utils.basicAuth(username, password));
}
app.use('/', function(req, res, next) {
return res.redirect(301, 'https://dwp-design-examples.herokuapp.com')
})
// Application settings
app.set('view engine', 'html');
app.set('views', [__dirname + '/app/views', __dirname + '/lib/']);
nunjucks.setup({
autoescape: true,
watch: true,
noCache: true
}, app);
nunjucks.ready(function(nj) {
// iterate over filter items and add each to nunjucks
Object.keys(filters.items).forEach(function(filterName) {
nj.addFilter(filterName, filters.items[filterName]);
});
});
// Middleware to serve static assets
app.use('/public', express.static(__dirname + '/public'));
app.use('/public', express.static(__dirname + '/govuk_modules/govuk_template/assets'));
app.use('/public', express.static(__dirname + '/govuk_modules/govuk_frontend_toolkit'));
app.use('/public/images/icons', express.static(__dirname + '/govuk_modules/govuk_frontend_toolkit/images'));
// Elements refers to icon folder instead of images folder
app.use(favicon(path.join(__dirname, 'govuk_modules', 'govuk_template', 'assets', 'images','favicon.ico')));
// Support for parsing data in POSTs
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Add variables that are available in all views
app.use(function (req, res, next) {
_.merge(res.locals,{
asset_path: '/public/',
serviceName: config.serviceName,
cookieText: config.cookieText,
releaseVersion: 'v' + releaseVersion,
postData: (req.body ? req.body : false)
});
next();
});
// routes (found in app/routes.js)
if (typeof(routes) != "function"){
console.log(routes.bind);
console.log("Warning: the use of bind in routes is deprecated - please check the prototype kit documentation for writing routes.")
routes.bind(app);
} else {
app.use("/", routes);
}
// auto render any view that exists
app.all(/^\/([^.]+)$/, function (req, res) {
var path = (req.params[0]);
res.render(path, function(err, html) {
if (err) {
res.render(path + "/index", function(err2, html) {
if (err2) {
console.log(err);
res.status(404).send(err + "<br>" + err2);
} else {
res.end(html);
}
});
} else {
res.end(html);
}
});
});
console.log("\nGOV.UK Prototype kit v" + releaseVersion);
// Display warning not to use kit for production services.
console.log("\nNOTICE: the kit is for building prototypes, do not use it for production services.");
// start the app
utils.findAvailablePort(app, function(port) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port);
if (env === 'production') {
app.listen(port);
} else {
app.listen(port-50,function()
{
console.log('got here')
browserSync({
proxy:'localhost:'+(port-50),
port:port,
ui:false,
files:['public/**/*.*','app/views/**/*.*'],
ghostmode:false,
open:false,
notify:false,
logLevel: "error"
});
});
}
});