Skip to content

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmaleigh committed Jan 13, 2016
1 parent beeed6a commit 81f0b18
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var path = require('path'),
var path = require('path'),
express = require('express'),
nunjucks = require('express-nunjucks'),
routes = require(__dirname + '/app/routes.js'),
favicon = require('serve-favicon'),
app = express(),
basicAuth = require('basic-auth'),
basicAuth = require('basic-auth-connect'),
bodyParser = require('body-parser'),
config = require(__dirname + '/app/config.js'),
port = (process.env.PORT || config.port),
Expand All @@ -15,28 +16,32 @@ var path = require('path'),
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
// 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));
if (env === 'production') {
if (!username || !password) {
console.log('Username or password is not set, exiting.');
process.exit(1);
}
app.use(basicAuth(username, password));
}

// Application settings
app.engine('html', require(__dirname + '/lib/template-engine.js').__express);
app.set('view engine', 'html');
app.set('vendorViews', __dirname + '/govuk_modules/govuk_template/views/layouts');
app.set('views', path.join(__dirname, '/app/views'));
app.set('views', [__dirname + '/app/views', __dirname + '/lib/']);

nunjucks.setup({
autoescape: true,
watch: true
}, app);

// 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

// 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
Expand All @@ -47,7 +52,7 @@ app.use(bodyParser.urlencoded({

// send assetPath to all views
app.use(function (req, res, next) {
res.locals.assetPath="/public/";
res.locals.asset_path="/public/";
next();
});

Expand All @@ -58,7 +63,6 @@ app.use(function (req, res, 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.")
Expand All @@ -68,15 +72,20 @@ if (typeof(routes) != "function"){
}

// auto render any view that exists

app.get(/^\/([^.]+)$/, function (req, res) {

var path = (req.params[0]);

res.render(path, function(err, html) {
if (err) {
console.log(err);
res.sendStatus(404);
res.render(path + "/index", function(err2, html) {
if (err2) {
console.log(err);
res.status(404).send(err).send(err2);
} else {
res.end(html);
}
});
} else {
res.end(html);
}
Expand All @@ -85,7 +94,6 @@ app.get(/^\/([^.]+)$/, function (req, res) {
});

// start the app

app.listen(port);
console.log('');
console.log('Listening on port ' + port);
Expand Down

0 comments on commit 81f0b18

Please sign in to comment.