Skip to content

Commit

Permalink
Move listener to listen-on-port.js
Browse files Browse the repository at this point in the history
This listener which looks out for file changes using BrowserSync doesn't need
to be part of `server.js` as it's only needed for npm start. We added a new file called
`listen-on-port.js` - this needs to be separate from `server.js` for Jest to work
correctly.

More detail here:
http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/#2-Separate-your-app-and-sever
  • Loading branch information
aliuk2012 authored and hannalaakso committed Nov 20, 2018
1 parent fb0c8e3 commit 0ccbc4a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gulp/nodemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const onQuit = () => {
gulp.task('server', function () {
nodemon({
watch: ['.env', '**/*.js', '**/*.json'],
script: 'server.js',
script: 'listen-on-port.js',
ignore: [
config.paths.public + '*',
config.paths.assets + '*',
Expand Down
32 changes: 32 additions & 0 deletions listen-on-port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// NPM dependencies
const browserSync = require('browser-sync')

// Local dependencies
const server = require('./server.js')
const config = require('./app/config.js')
const utils = require('./lib/utils.js')

// Set up configuration variables
var useBrowserSync = config.useBrowserSync.toLowerCase()
var env = (process.env.NODE_ENV || 'development').toLowerCase()

utils.findAvailablePort(server, function (port) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
if (env === 'production' || useBrowserSync === 'false') {
server.listen(port)
} else {
server.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'
})
})
}
})
55 changes: 15 additions & 40 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path')

// NPM dependencies
const bodyParser = require('body-parser')
const browserSync = require('browser-sync')
const dotenv = require('dotenv')
const express = require('express')
const nunjucks = require('nunjucks')
Expand Down Expand Up @@ -58,13 +57,11 @@ var useAuth = process.env.USE_AUTH || config.useAuth
var useAutoStoreData = process.env.USE_AUTO_STORE_DATA || config.useAutoStoreData
var useCookieSessionStore = process.env.USE_COOKIE_SESSION_STORE || config.useCookieSessionStore
var useHttps = process.env.USE_HTTPS || config.useHttps
var useBrowserSync = config.useBrowserSync
var gtmId = process.env.GOOGLE_TAG_MANAGER_TRACKING_ID

env = env.toLowerCase()
useAuth = useAuth.toLowerCase()
useHttps = useHttps.toLowerCase()
useBrowserSync = useBrowserSync.toLowerCase()

var useDocumentation = (config.useDocumentation === 'true')

Expand Down Expand Up @@ -96,12 +93,19 @@ var appViews = [
path.join(__dirname, '/lib/')
]

var nunjucksAppEnv = nunjucks.configure(appViews, {
var nunjucksConfig = {
autoescape: true,
express: app,
noCache: true,
watch: true
})
watch: false // We are now setting this to `false` (it's by default false anyway) as having it set to `true` for production was making the tests hang
}

if (env === 'development') {
nunjucksConfig.watch = true
}

nunjucksConfig.express = app

var nunjucksAppEnv = nunjucks.configure(appViews, nunjucksConfig)

// Add Nunjucks filters
utils.addNunjucksFilters(nunjucksAppEnv)
Expand All @@ -125,12 +129,8 @@ if (useDocumentation) {
path.join(__dirname, '/lib/')
]

var nunjucksDocumentationEnv = nunjucks.configure(documentationViews, {
autoescape: true,
express: documentationApp,
noCache: true,
watch: true
})
nunjucksConfig.express = documentationApp
var nunjucksDocumentationEnv = nunjucks.configure(documentationViews, nunjucksConfig)
// Nunjucks filters
utils.addNunjucksFilters(nunjucksDocumentationEnv)

Expand All @@ -151,13 +151,9 @@ if (useV6) {
path.join(__dirname, '/app/v6/views/'),
path.join(__dirname, '/lib/v6') // for old unbranded template
]
nunjucksConfig.express = v6App
var nunjucksV6Env = nunjucks.configure(v6Views, nunjucksConfig)

var nunjucksV6Env = nunjucks.configure(v6Views, {
autoescape: true,
express: v6App,
noCache: true,
watch: true
})
// Nunjucks filters
utils.addNunjucksFilters(nunjucksV6Env)

Expand Down Expand Up @@ -350,25 +346,4 @@ app.use(function (err, req, res, next) {
console.log('\nGOV.UK Prototype Kit v' + releaseVersion)
console.log('\nNOTICE: the kit is for building prototypes, do not use it for production services.')

// Find a free port and start the server
utils.findAvailablePort(app, function (port) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
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

0 comments on commit 0ccbc4a

Please sign in to comment.