This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (58 loc) · 1.58 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
const server = require('umi-server');
const http = require('http');
const { createReadStream } = require('fs');
const { join, extname } = require('path');
const fetch = require('node-fetch');
const root = join(__dirname, 'dist');
const render = server({
root,
});
const headerMap = {
'.js': 'text/javascript',
'.css': 'text/css',
'.jpg': 'image/jpeg',
'.png': 'image/jpeg',
};
const createServer = http.createServer(async (req, res) => {
const ext = extname(req.url);
const header = {
'Content-Type': headerMap[ext] || 'text/html',
};
res.writeHead(200, header);
const reqHeaderAccept = req.headers["accept"];
if (reqHeaderAccept && reqHeaderAccept.includes('text/html')) {
// url render
const ctx = {
req,
res,
};
const renderRes = await render(ctx);
const { ssrHtml } = renderRes;
res.write(ssrHtml);
res.end();
} else {
if(process.env.NODE_ENV==='development'){
const upstreamUrl = new URL(req.url, 'http://localhost:8000');
const upstreamRes = await fetch(upstreamUrl, {
headers: req.headers,
method: req.method
});
// const upstreamText = await upstreamRes.text();
upstreamRes.body.pipe(res);
}else {
// static file url
const path = join(root, req.url);
const stream = createReadStream(path);
stream.on('error', error => {
res.writeHead(404, 'Not Found');
res.end();
});
stream.pipe(res);
}
}
});
if (!process.env.NOW_ZEIT_ENV) {
createServer.listen(3000);
console.log('http://localhost:3000');
}
module.exports = createServer;