forked from alphagov/govuk-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
117 lines (101 loc) · 3.13 KB
/
app.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
const express = require('express')
const app = express()
const nunjucks = require('nunjucks')
const fs = require('fs')
const path = require('path')
const port = (process.env.PORT || 3000)
const herokuApp = process.env.HEROKU_APP
const dto = require('directory-to-object')
// Set up views
const appViews = [
path.join(__dirname, '/src/views/'),
path.join(__dirname, '/src/components/'),
path.join(__dirname, '/src/examples/')
]
// Configure nunjucks
nunjucks.configure(appViews, {
autoescape: true,
express: app,
noCache: true,
watch: true
})
// Set view engine
app.set('view engine', 'njk')
// Set up middleware to serve static assets
app.use('/public', express.static(path.join(__dirname, '/public')))
app.use('/icons', express.static(path.join(__dirname, '/public/icons')))
app.listen(port, () => {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
})
// Routes
// Return an object representing the components directory
dto(path.resolve('./src/components'), (err, res) => {
if (err) {
console.log(err)
}
app.locals.componentsDirectory = res
})
// Index page - render the component list template
app.get('/', function (req, res) {
res.render('component-list')
})
// Examples
app.get('/examples*', function (req, res) {
res.render('index')
})
// Components
app.get('/components*', function (req, res) {
var path = (req.params[0]).replace(/\//g, '')
// If it isn't the isolated preview, render the component "detail" page
if (path.indexOf('preview') === -1) {
try {
var componentNjk = fs.readFileSync('src/components/' + path + '/' + path + '.njk', 'utf8')
var componentHtml = fs.readFileSync('src/components/' + path + '/' + path + '.html', 'utf8')
res.locals.componentPath = path
res.locals.componentNunjucksFile = componentNjk
res.locals.componentHtmlFile = componentHtml
} catch (e) {
console.log('Error:', e.stack)
}
res.render(path, {
componentPath: res.locals.componentPath,
componentNunjucksFile: res.locals.componentNunjucksFile,
componentHtmlFile: res.locals.componentHtmlFile
},
function (err, html) {
if (err) {
res.render(path + '/' + 'index', function (err2, html) {
if (err2) {
res.status(404).send(err + '<br>' + err2)
} else {
res.end(html)
}
})
} else {
res.end(html)
}
})
} else {
// Show the isolated component preview
path = path.replace(/preview/g, '')
res.locals.componentPath = path
res.render('component-preview')
}
})
// Config for Heroku
// If this is the Heroku demo app
if (herokuApp === 'DEMO') {
app.use('/', express.static(path.join(__dirname, 'demo')))
}
// Disallow search index indexing
app.use(function (req, res, next) {
// none - Equivalent to noindex, nofollow
// noindex - Do not show this page in search results and do not show a "Cached" link in search results.
// nofollow - Do not follow the links on this page
res.setHeader('X-Robots-Tag', 'none')
next()
})
app.get('/robots.txt', function (req, res) {
res.type('text/plain')
res.send('User-agent: *\nDisallow: /')
})