-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
55 lines (48 loc) · 1.66 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
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
import express from 'express'
import { createServer as createViteServer } from 'vite'
/* const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const config = require('./webpack.config') */
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const distDir = path.join(__dirname, 'dist')
const htmlFile = path.join(distDir, 'index.html')
const isDevelopment = process.env.NODE_ENV !== 'production'
const app = express()
const port = 3000
app.use('/fakeData', express.static('test/fakeData'))
async function startServer() {
/* if (isDevelopment) { */
const vite = await createViteServer({
server: { middlewareMode: 'html' }
});
app.use(vite.middlewares);
app.get('*', async (req, res, next) => {
try {
const url = req.originalUrl;
let template = fs.readFileSync(
path.resolve(__dirname, 'index.html'),
'utf-8',
)
const { render } = await vite.ssrLoadModule('/src/entry-server.js');
const appHtml = await render(url);
const html = template.replace(`<!--ssr-outlet-->`, appHtml);
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
} catch (e) {
vite.ssrFixStacktrace(e);
next(e);
}
});
app.listen(port, function(error) {
if (error) {
console.error(error)
} else {
console.info("==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
}
});
}
startServer();