如何从 Koa.js 服务器发送 React 应用程序?
How can I send a React app from a Koa.js server?
我有 React 应用程序。我有 bundle.js
和 index.html
以及脚本和根元素。我如何从服务器发送它?
我有一个 Koa.js 服务器
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
我可以将 index.html
作为静态文件发送吗?或者 React 有其他方式吗?
很简单。您需要安装 koa-static。还有一个用于静态文件的文件夹,例如 public
,您需要在那里构建您的 reactjs 应用程序。然后将 koa-static 中间件添加到你的服务器,这里是它的要点:
const Koa = require('koa')
const serve = require('koa-static')
const app = new Koa();
app.use(serve(__dirname + '/public'))
app.listen(3000)
我有 React 应用程序。我有 bundle.js
和 index.html
以及脚本和根元素。我如何从服务器发送它?
我有一个 Koa.js 服务器
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
我可以将 index.html
作为静态文件发送吗?或者 React 有其他方式吗?
很简单。您需要安装 koa-static。还有一个用于静态文件的文件夹,例如 public
,您需要在那里构建您的 reactjs 应用程序。然后将 koa-static 中间件添加到你的服务器,这里是它的要点:
const Koa = require('koa')
const serve = require('koa-static')
const app = new Koa();
app.use(serve(__dirname + '/public'))
app.listen(3000)