CategoriesUncategorized

Using Node.js, to serve Vue.js full breakdown

Dockerfile:

FROM node:alpine

MAINTAINER Joseph Shinaberry <joseph.shinaberry@spathesystems.com>

WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY . /app

USER root
RUN apk add --no-cache --virtual .gyp \
        python \
        make \
        g++ \
    && npm install \
        npm install @vue/cli -g \
    && apk del .gyp \
    && vue-cli-service build

RUN chown -R 1001:0 /app
USER 1001

# OPEN PORT
EXPOSE 8080

# start app
CMD [ "node", "server.js" ]

Dependencies add to package.json

        "connect-history-api-fallback": "^1.6.0",
        "express": "^4.15.2",

Server.js file

const express = require('express');
const path = require('path');
const history = require('connect-history-api-fallback');

const app = express();

const staticFileMiddleware = express.static(path.join(__dirname + '/dist'));

app.use(staticFileMiddleware);
app.use(history({
  disableDotRule: true,
  verbose: true
}));
app.use(staticFileMiddleware);

app.get('/', function (req, res) {
  res.render(path.join(__dirname + '/dist/index.html'));
});

var server = app.listen(process.env.PORT || 8080, function () {
  var port = server.address().port;
  console.log("App now running on port", port);
});

Leave a Reply

Your email address will not be published. Required fields are marked *