Using react you need to serve index.html with react_app.js included in it at any route if user have not downloaded react_app.js (came first time).
Then you need to serve some api calls from react_app.js, but if you use same url for GET, let's say, you will get API call response and not the index.html with react_app.js.
What is the solution for this? Make api calls only with some prefix and send index.html only if route not found?
My code:
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!!!!')
}
})
/api/prefix. It's easier that way, because sometimes setting the header is not as easy as just using a different route.