forked from jacques/docs.npmjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (97 loc) · 2.86 KB
/
index.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
if (typeof GLOBAL.Promise === 'undefined') GLOBAL.Promise = require('bluebird')
var express = require('express')
var hbs = require('hbs')
var harp = require('harp')
var path = require('path')
var cors = require('cors')
var find = require('lodash').find
var merge = require('lodash').merge
var sortBy = require('lodash').sortBy
var any = require('lodash').any
var suggest = require(__dirname + '/lib/suggestions')
var redirects = require(__dirname + '/lib/redirects')
// Load section and page data
var content = require(path.resolve(__dirname, 'content.json'))
// Copy pages into their sections and sort them by order or title
content.sections.forEach(function (section) {
section.pages = content.pages.filter(function (page) {
return section.id === page.section
})
// Sort section pages if any of the pages have an `order` property
// Pages without the order property will come last
if (any(section.pages, 'order')) {
section.pages = sortBy(section.pages, function (page) {
return Number(page.order || 10000)
})
}
})
var lite = merge({}, content)
lite.pages = lite.pages.map(function (page) {
delete page.content
return page
})
// Configure Express
var app = module.exports = express()
app.set('view engine', 'hbs')
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname + '/public'))
app.use(harp.mount(__dirname + '/public'))
hbs.registerPartials(__dirname + '/views/partials')
hbs.registerHelper('equal', require('handlebars-helper-equal'))
app.get('/', function (req, res) {
res.render('index', {
content: content,
recentlyUpdatedPages: [],
pageId: 'index'
})
})
app.get('/_monitor/ping', cors(), function (req, res) {
res.status(200).send('pong')
})
app.get('/_monitor/status', cors(), function (req, res) {
res.json({
name: 'docs',
pid: process.pid,
uptime: process.uptime(),
rss: process.memoryUsage()
})
})
app.get('/content.json', cors(), function (req, res) {
res.json(content)
})
app.get('/content.lite.json', cors(), function (req, res) {
res.json(lite)
})
app.get('/all', function (req, res) {
res.render('multi', {
content: content,
heading: 'All Docs'
})
})
app.get('/*', function (req, res) {
if (req.path in redirects) {
return res.redirect(301, redirects[req.path])
}
var page = find(content.pages, function (page) {
return page.href === req.path
})
if (!page) {
return res.status(404).render('404', {
url: req.url,
pageId: 'fourohfour',
content: content,
suggestions: suggest(req.path, content.pages)
})
}
res.render('page', {
page: page,
content: content
})
})
// This module.parent thing allows us to test the server using
// supertest without unnecessarily firing up the server.
if (!module.parent) {
app.listen(app.get('port'), function () {
console.log('Running at localhost:' + app.get('port'))
})
}