I've written simple todo-app and I'm trying to deploy it to Heroku. I deployed on heroku before, I used Express + React. In my server.js file, there was that piece of code:
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
That piece of code serves 'build' folder if app is in production, and also sends 'index.html' to every request.
I need similar code but for Koa + React. Koa is very minimalistic framework, so I guess there must be additional packages installed, I don't know which ones. I tried koa-send and koa-static, but couldn't configure them. How do I do that?
I can't test this out right now but I think it would go something like this:
const Koa = require('koa');
const Router = require('koa-router');
const send = require('koa-send');
const static = require('koa-static');
const app = new Koa();
const router = new Router();
if (process.env.NODE_ENV === 'production') {
app.use(static('./client/build'));
router.get('*', async (ctx, next) => {
try {
await send(ctx, './client/build/index.html');
} catch(err) {
// TODO: handle err?
return next();
}
});
}
app.listen(3000);