forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (97 loc) · 3.32 KB
/
server.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
'use strict'
// The server where the site is exposed through a static file server
// while developing locally.
const path = require('path')
const st = require('st')
const http = require('http')
const chokidar = require('chokidar')
const mount = st({
path: path.join(__dirname, 'build'),
cache: false,
index: 'index.html',
passthrough: true
})
const build = require('./build')
const port = process.env.PORT || 8080
// Watches for file changes in the locale, layout and static directories, and
// rebuilds the modified one.
const opts = {
persistent: true,
ignoreInitial: true,
followSymlinks: true,
usePolling: true,
alwaysStat: false,
depth: undefined,
interval: 100,
ignorePermissionErrors: false,
atomic: true
}
const locales = chokidar.watch(path.join(__dirname, 'locale'), opts)
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
const statics = chokidar.watch(path.join(__dirname, 'static'), opts)
const fs = require('fs')
// Read all the langs under `locale`
const SUPPORTED_LANGUAGES = new Set(fs.readdirSync(path.join(__dirname, 'locale')))
// Redirect mechanism meant as a fix for languages where some pages
// have not been translated yet, therefore redirect to the english equivalent,
// which isn't the correct language, but better than a 404-page
function redirectToEnglishUrl (req, res) {
return () => {
// Url should be case insensitive.(e.g: zh-CN = zh-cn),
// So we should make a convert to the lower case and check the route values.
let url = req.url.toLowerCase()
const splitedValues = url.split('/')
// For urls like `/blog`, add `en` before that
if (splitedValues.length === 2) {
splitedValues[0] = 'en'
url = splitedValues.join('/').trim()
} else if (splitedValues[1] !== 'en' && SUPPORTED_LANGUAGES.has(splitedValues[1])) {
// For urls like `/lang/docs/`.
// If we found the lang in our set, this means the specific lang
// doesn't have proper translated pages yet, so force the default
// lang to `en`.
splitedValues[1] = 'en'
url = splitedValues.join('/').trim()
}
res.writeHead(302, {
location: url
})
res.end()
}
}
// Gets the locale name by path.
function getLocale (filePath) {
const pre = path.join(__dirname, 'locale')
return filePath.slice(pre.length + 1, filePath.indexOf('/', pre.length + 1))
}
build.getSource((err, source) => {
if (err) { throw err }
locales.on('change', (filePath) => {
build.buildLocale(source, getLocale(filePath))
})
locales.on('add', (filePath) => {
build.buildLocale(source, getLocale(filePath))
locales.add(filePath)
})
})
layouts.on('change', build.fullBuild)
layouts.on('add', (filePath) => {
layouts.add(filePath)
build.fullBuild()
})
statics.on('change', build.copyStatic)
statics.on('add', (filePath) => {
statics.add(filePath)
build.copyStatic()
})
// Initializes the server and mounts it in the generated build directory.
http.createServer((req, res) => {
// If we are accessing the root, it should be redirected to `/en` instead.
// We shouldn't get a 404 page.
if (req.url === '/') {
req.url = '/en'
}
mount(req, res, redirectToEnglishUrl(req, res))
}).listen(port, () => console.log(`\x1B[32mServer running at http://localhost:${port}/en/\x1B[39m`))
// Start the initial build of static HTML pages
build.fullBuild()