This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
89 lines (76 loc) · 2.45 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
const fs = require('fs')
const next = require('next')
const util = require('util')
const express = require('express')
const pathToRegExp = require('path-to-regexp')
const { parse } = require('url')
const routes = require('./route')
const uglifyjs = require('uglify-es')
const server = express()
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handler = routes.getRequestHandler(app)
const port = 3000
const md5 = text => require('crypto').createHash('md5').update(text).digest('hex')
const routeString = fs.readFileSync(__dirname + '/route.js', 'utf-8').split('/* ========= */')[1]
const clientSideScript = (() => {
const doGenerateRoute = (routeString) => {
const routes = {}
const ret = []
routes.add = (name, pattern, component) => {
let keys = []
const re = pathToRegExp(pattern, keys)
ret.push([component, re, keys])
}
eval(routeString)
return ret
}
const routes = doGenerateRoute(routeString)
const code = `
(function () {
function match (item, path) {
const values = item[1].exec(path)
if (values) {
return valuesToParams(item[2], values.slice(1))
}
}
function valuesToParams (keys, values) {
return values.reduce((params, val, i) => {
if (val === undefined) return params
return Object.assign(params, {
[keys[i].name]: decodeURIComponent(val)
})
}, {})
}
var routes = ${util.inspect(routes, {depth: null})};
for (var i = 0; i < routes.length; i++) {
var route = routes[i];
var a = match(route, location.pathname)
if (a) {
window.__NEXT_DATA__.page = '/' + route[0]
window.__NEXT_DATA__.query = a
console.log(window.__NEXT_DATA__.page, window.__NEXT_DATA__.query)
return
}
}
window.__NEXT_DATA__.page = '/_error'
})();
`
return uglifyjs.minify(code).code
})()
const hashClientSideScript = md5(clientSideScript)
const clientSideScriptFileName = `/_next/static/client.${hashClientSideScript}.js`
app.prepare().then(() => {
server.use((req, res, next) => {
req.clientSideScript = clientSideScriptFileName
next()
})
server.get(clientSideScriptFileName, (req, res) => {
return res.end(clientSideScript)
})
server.use(handler)
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`) // eslint-disable-line
})
})