-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
176 lines (147 loc) · 4.64 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const fs = require('fs')
const path = require('path')
const http = require('http')
const querystring = require('querystring')
const express = require('express')
//const favicon = require('serve-favicon')
const cookieParser = require('cookie-parser')
const bodyParser = require("body-parser")
//const LRU = require('lru-cache')
const {createBundleRenderer} = require('vue-server-renderer')
//const metaConfig = require("./meta-config")
// const isProd = true// process.env.NODE_ENV === 'production'
const isProd = false
const config = {
general: {
// host: isProd ? 'api.120xinmao.com' : '192.168.0.247',
// host: isProd ? 'api.120xinmao.com' : '127.0.0.1',
host: isProd ? 'moluantongxue.com' : '127.0.0.1',
port: isProd ? 80 : 9002
},
testing: {
// host: isProd ? 'api.120xinmao.com' : '192.168.0.247',
// host: isProd ? 'api.120xinmao.com' : '127.0.0.1',
host: isProd ? 'moluantongxue.com' : '127.0.0.1',
port: 9000
},
templatePath: isProd ? './dist/index.template.html' : './src/index.template.html'
}
const resolve = file => path.resolve(__dirname, file)
const app = express()
function createRenderer (bundle, options) {
return createBundleRenderer(bundle, Object.assign(options, {
// recommended for performance
runInNewContext: false
}))
}
let renderer
let readyPromise
const templatePath = resolve(config.templatePath)
const template = fs.readFileSync(templatePath, 'utf-8')
const bundle = require('./dist/vue-ssr-server-bundle.json')
const clientManifest = require('./dist/vue-ssr-client-manifest.json')
renderer = createRenderer(bundle, {
template,
clientManifest
})
app.set("x-powered-by", false)
app.use(cookieParser('hanyang'))
//app.use(favicon(path.resolve(__dirname, 'public/favicon.png')))
app.use('/static', express.static(path.join(__dirname, "dist/static")))
app.use(bodyParser.urlencoded({
extended: true
}))
const handleError = (err, req, res) => {
if (err.url) {
res.redirect(err.url)
} else if(err.code === 404) {
res.status(404).send('404 | Page Not Found')
} else {
// Render Error Page or Redirect
res.status(500).send('500 | Internal Server Error')
console.error(`error during render : ${req.url}`)
console.error(err.stack)
}
}
// const microCache = LRU({
// max: 100,
// maxAge: 10 * 60 * 1000 // 10分钟后过期
// })
function render (req, res) {
// const hit = microCache.get(req.url)
// if (hit) {
// return res.end(hit)
// }
res.setHeader("Content-Type", "text/html")
const context = {
url: req.url,
uid: req.cookies.mid || req.signedCookies.uid,
webp: req.headers.accept.includes('webp') || req.cookies.webp
}
renderer.renderToString(context, (err, html) => {
if (err) {
return handleError(err, req, res)
}
res.send(html)
//microCache.set(req.url, html)
})
}
app.get("/depression-testing/*", (req, res) => {
//const postData = querystring.stringify(req.query)
const proxyRequest = http.request({
host: config.testing.host,
port: config.testing.port,
path: req.path.replace("/depression-testing", ""),
method: req.method,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
//"Content-Length": Buffer.byteLength(postData)
}
}, (proxyResponse) => {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers)
proxyResponse.pipe(res)
})
//proxyRequest.write(postData)
proxyRequest.end()
})
app.post("/depression-testing/*", (req, res) => {
const postData = querystring.stringify(req.body)
const proxyRequest = http.request({
host: config.testing.host,
port: config.testing.port,
path: req.path.replace("/depression-testing", ""),
method: req.method,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(postData)
}
}, (proxyResponse) => {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers)
proxyResponse.pipe(res)
})
proxyRequest.write(postData)
proxyRequest.end()
})
app.post("/depression-api/*", (req, res) => {
const postData = querystring.stringify(req.body)
const proxyRequest = http.request({
host: config.general.host,
port: config.general.port,
path: req.path,
method: req.method,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(postData)
}
}, (proxyResponse) => {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers)
proxyResponse.pipe(res)
})
proxyRequest.write(postData)
proxyRequest.end()
})
app.get('*', render)
const port = 6006
app.listen(port, () => {
console.log(`[express] server started at localhost:${port}\n`)
})