[Vuejs]-Vue with own backend code on server (Node.js+Vue.js)

0👍

As Ifaruki commented the result of the vue-cli in the end is the dist-folder. In the folder there’s a index.html and all the files, like js and css, that’s needed to run the frontend.

How you serve this index.html in the end is up to you, so for example, if your backend runs Express, you can build you Vue-dist-folder to somewhere your Express application can reach it and serve it when the user tries to reach it.

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

This will serve the page when the user GET the / route. This is a simple example but it’s the generall idea.

Another option would be to maybe run Vue Nuxt instead of Vue. Nuxt is a server-side-rendered framework that uses Vue. This means that you could build it more into the backend and maybe split into a /web and /api namespace for the routes.

Since it’s generally not possible to run two processes on the same port this kind of solution is the only one i can think of.

Leave a comment