12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- // import express from 'express';
- const express = require('express');
- const app = express();
- app.use(express.static('./'));
- app.use(async (req, res) => {
- // 或者从 CDN 上下载到 server 端
- // const serverPath = await downloadServerBundle('http://cdn.com/bar/umi.server.js');
- const render = require('./umi.server');
- res.setHeader('Content-Type', 'text/html');
- var _html = `
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta
- name="viewport"
- content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
- />
- <link rel="stylesheet" href="/umi.css" />
- <script>
- window.routerBase = "/";
- </script>
- <script>
- //! umi version: 3.5.37
- </script>
- </head>
- <body>
- <div id="root"></div>
- <script src="/umi.js"></script>
- </body>
- </html>
- `;
- const context = {};
- const { html, error, rootContainer } = await render({
- // 有需要可带上 query
- path: req.url,
- context,
- // 可自定义 html 模板
- htmlTemplate: _html,
- // 启用流式渲染
- // mode: 'stream',
- // html 片段静态标记(适用于静态站点生成)
- // staticMarkup: false,
- // 扩展 getInitialProps 在服务端渲染中的参数
- // getInitialPropsCtx: {},
- // manifest,正常情况下不需要
- });
- // support stream content
- res.send(html);
- // if (content instanceof Stream) {
- // html.pipe(res);
- // html.on('end', function () {
- // res.end();
- // });
- // } else {
- // res.send(res);
- // }
- });
- app.listen(8088);
|