我正在使用koa-static来为root url localhost:3000/ 提供静态文件,并使用koa-router来提供RESTful api,如 localhost:3000/api/account/login

使 fakeBuild 文件夹只包含一个文件 index.html . 一切正常 . root url和api url显示正确的内容 .

// serve static file
const serve = require('koa-static')
const staticPath = path.join(__dirname,'..','Client','poker','fakeBuild')
log(staticPath)
app.use(serve(staticPath))

// router
const router = require('./router/Index.js')(app)

然后我尝试将Koa与React连接起来

我使用 yarn build 来构建React静态文件,React项目由 create-react-app 创建,未经修改 .

然后我将koa-static的目标更改为 build 文件夹,只更改了一行

const staticPath = path.join(__dirname,'..','Client','poker','build')

然后访问 localhost:3000/ ,chrome show React start page,正常工作

然后访问 localhost:3000/api/account/login ,chrome show React start page,what ??

使用邮差检查 localhost:3000/api/account/login ,它响应我放入路由器的内容,很好 .

所以我可以使用apis,只是忽略chrome的结果?

它必须是React和Chrome做一些额外的事情 . 我尝试在React构建文件夹中删除 service-worker.js ,api url在chrome中正确呈现 .

所以任何人都可以提供一些文件或解释,以帮助我了解做了什么反应,以保持所有的网址呈现它 .

为什么我的api localhost:3000/api/account/login 不需要 service-worker.js (只是在响应体中返回一些文本),也让js工作了 . 只是因为 koa-static 服务了文件?

====

更新:我的路由器代码和项目here

路由器/ Index.js

const Router = require('koa-router');

const R = (app)=>{
  const router = new Router();


  const api = require('./Api.js');
  router.use('/api', api.routes(), api.allowedMethods())


  app.use(router.routes())
    .use(router.allowedMethods());
};

module.exports = R;

路由器/ Api.js

const Router = require('koa-router')
const log = require('../helper/Utils').log;

module.exports = (function () {
  const Api = new Router();
  Api.get('/account/login', async (ctx, next)=>{
    log("hello");
    ctx.status = 200;
    ctx.body = `login`
  });

  Api.get('/account/register', async (ctx, next)=>{
    ctx.status = 200;
    ctx.body = `register`

  });

  return Api;
})();