-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-old.js
137 lines (116 loc) · 3.41 KB
/
server-old.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
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
/**
* .env file
*/
require('dotenv').config()
/**
* Module dependencies
*/
const express = require('express')
const path = require('path')
const handlebars = require('express-handlebars')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
// const debug = require('debug')('nhsuk-prototype-kit:server')
// const http = require('http')
const chalk = require('chalk')
const browserSync = require('browser-sync')
const favicon = require('serve-favicon')
/**
* Config
*/
const config = require('./config.js').app
/**
* Utils
*/
const utils = require('./lib/utils.js')
let username = process.env.USERNAME
let password = process.env.PASSWORD
let env = process.env.NODE_ENV || 'development'
const useBrowserSync = config.useBrowserSync.toLowerCase()
const useAuth = process.env.USE_AUTH || config.useAuth.toLowerCase()
env = env.toLowerCase()
const router = express.Router()
const routes = require('./app/routes')
let app = express()
// Authenticate against the environment-provided credentials, if running
// the app in production
if (env === 'production' && useAuth === 'true') {
app.use(utils.productionAuth(username, password))
}
// set view engine to handlebars
const hbs = handlebars.create({
extname: '.hbs',
defaultLayout: 'nhsuk_layout',
layoutsDir: 'app/views/_layouts',
partialsDir: 'app/views/_partials'
})
app.engine('.hbs', hbs.engine)
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'app', 'views'))
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
// Strip .html and .htm if provided
app.get(/\.html?$/i, function (req, res) {
var path = req.path
var parts = path.split('.')
parts.pop()
path = parts.join('.')
res.redirect(path)
})
// Auto render any view that exists
routes(router, hbs)
app.use('/', router)
// Strip .html and .htm if provided
app.get(/\.html?$/i, function (req, res) {
var path = req.path
var parts = path.split('.')
parts.pop()
path = parts.join('.')
res.redirect(path)
})
// Auto render any view that exists
// App folder routes get priority
app.get(/^\/([^.]+)$/, function (req, res) {
utils.matchRoutes(req, res)
})
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// render the error page
res.status(err.status || 500)
res.render('error')
})
utils.findAvailablePort(app, function (port) {
console.log(chalk.bgHex('#005eb8').bold('\n NHSUK prototype is now running \n'))
console.log('Visit http://localhost:' + port + ' in your browser to view')
if (env === 'production' || useBrowserSync === 'false') {
app.listen(port)
} else {
app.listen(port - 50, function () {
browserSync({
proxy: 'localhost:' + (port - 50),
port: port,
ui: false,
files: ['public/**/*.*', 'app/views/**/*.*'],
ghostmode: false,
open: false,
notify: false,
logLevel: 'error'
})
})
}
})
module.exports = app