forked from iterative/dvc.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (56 loc) · 1.79 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
/* eslint-env node */
/*
* Custom server (with custom routes) See
* https://nextjs.org/docs/advanced-features/custom-server
*
* NOTE: This file doesn't go through babel or webpack. Make sure the syntax and
* sources this file requires are compatible with the current node version you
* are running.
*/
const { createServer } = require('http')
const { parse } = require('url')
const { stringify } = require('querystring')
const next = require('next')
const { getItemByPath } = require('./src/utils/sidebar')
const { getRedirect } = require('./src/utils/redirects')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = process.env.PORT || 3000
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
const host = req.headers.host
res.setHeader('Cache-Control', 'public, max-age=0, s-maxage=99999')
let [redirectCode, redirectLocation] = getRedirect(host, pathname, {
req,
dev
})
if (redirectLocation) {
// HTTP redirects
const queryStr = stringify(query)
if (queryStr) {
redirectLocation += '?' + queryStr
}
res.writeHead(redirectCode, {
Location: redirectLocation
})
res.end()
} else if (/^\/doc(\/.*)?$/.test(pathname)) {
// Docs Engine handler
// Force 404 response code for any inexistent /doc item.
if (!getItemByPath(pathname)) {
res.statusCode = 404
}
// Custom route for all docs
app.render(req, res, '/doc', query)
} else {
// Regular Next.js handler
handle(req, res, parsedUrl)
}
}).listen(port, err => {
if (err) throw err
console.info(`> Ready on localhost:${port}`)
})
})