I have a working vue project using vue cli 3 rc3, and I followed one of the many tutorials online for creating an express server to serve my files on heroku; however, none of those tutorials say how I can have my shiny new server.js also work for dev, which I think would be the whole point of deploying to something like heroku instead of something geared to static assets.
Obviously I could ditch the npm run serve command that the cli provides and use just my own server, but then I lose the hot loading and other amazing features.
I could create a separate project (I'm guessing this is common), but I want my backend and frontend to share code.
I've been trying to figure this out for a week now, even resorting to reading the cli code on github to see how hard it would be to replicate.
Webpack provides the functionality via the webpack-dev-middleware, but when I try to use that as-is I get missing config errors and things don’t quite work – presumably because of the defaults that the vue cli provides that I’m missing.
I would think something like this should work...
const express = require('express');
const serveStatic = require('serve-static');
const path = require('path');
const app = express();
if (process.env.NODE_ENV === 'production') {
app.use('/', serveStatic(path.join(__dirname, '/dist')));
app.get('*', function(req, res) {
res.sendFile(__dirname + '/dist/index.html');
});
} else {
// TODO: Find/create some middleware that does
// everything that vue-cli-service serve does
// app.use(require('vue-dev-middleware'));
}
const port = process.env.PORT || 8080;
app.listen(port);
Does anyone have any guidance?