最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何以服务为index.html的反应,在处理相同的路径路由?

运维笔记admin22浏览0评论

如何以服务为index.html的反应,在处理相同的路径路由?

如何以服务为index.html的反应,在处理相同的路径路由?

使用反应你需要服务的index.html与包含在它react_app.js在任何途径,如果用户还没有下载react_app.js(来第一次)。

然后你需要为来自react_app.js一些API调用,但如果你使用相同的URL为GET,比方说,你会得到API呼叫响应,而不是用的index.html react_app.js。

,这是什么解决办法吗?进行API只能用一些前缀电话和发送的index.html只有在没有找到路径?

我的代码:

fastify.register(require('fastify-static'), {
  root: path.join(__dirname, './static')
})

fastify.route({
  method: 'GET',
  url: '/',
  handler: async (req, res) => {
    res.send('you will get api call answer but you need to serve index.html with react_app.js first!!!!')
  }
})
回答如下:

作为@Garrert建议,这是它如何工作:

// Статические файлы
fastify.register(require('fastify-static'), {
  root: path.join(__dirname, './static')
})

// this will work with fastify-static and send ./static/index.html
fastify.setNotFoundHandler((req, res) => {
  res.sendFile('index.html')
})

// Here go yours routes with prefix
fastify.register(async openRoutes => {
  openRoutes.register(require('./server/api/open'))
}, { prefix: '/api' })
发布评论

评论列表(0)

  1. 暂无评论