代码之家  ›  专栏  ›  技术社区  ›  ZiiMakc

如何服务索引.html对于相同路径上的反应和处理路由?

  •  0
  • ZiiMakc  · 技术社区  · 7 年前

    使用你需要的反应索引.html用react_应用程序js如果用户没有下载react,则在任何路径上都包括在内_应用程序js(第一次来)。

    解决这个问题的办法是什么?仅使用某些前缀进行api调用并发送索引.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!!!!')
      }
    })
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   ZiiMakc    7 年前

    // Статические файлы
    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' })
    
    推荐文章