VOOZH about

URL: https://dzone.com/articles/serving-a-vuejs-application-with-a-go-backend

โ‡ฑ Serving a Vue.js Application With a Go Backend


Related

  1. DZone
  2. Coding
  3. JavaScript
  4. Serving a Vue.js Application With a Go Backend

Serving a Vue.js Application With a Go Backend

In this guide, we embark on a journey to serve a fundamental Vue.js application, displaying 'Hello World !!', and employing a Go (Golang) HTTP server.

By May. 24, 24 ยท Tutorial
Likes
Comment
Save
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

In the ever-evolving realm of web development, JavaScript frameworks like Vue.js have become indispensable tools for crafting dynamic and responsive user interfaces. In this guide, we embark on a journey to serve a fundamental Vue.js application, displaying 'Hello World !!', and employing a Go (Golang) HTTP server.

Folder Structure

Here's the folder structure of the project:

first-vue-go-app/
โ”‚
โ”œโ”€โ”€ first-vue-app/ # Vue.js Application
โ”‚ โ”œโ”€โ”€ src/
โ”‚ โ”‚ โ”œโ”€โ”€ assets/ # Static assets like images, fonts, etc.
โ”‚ โ”‚ โ”œโ”€โ”€ components/ # Vue components
โ”‚ โ”‚ โ”œโ”€โ”€ views/ # Vue views or pages
โ”‚ โ”‚ โ”œโ”€โ”€ App.vue # Main Vue component
โ”‚ โ”‚ โ””โ”€โ”€ main.js # Vue application entry point
โ”‚ โ”œโ”€โ”€ public/ # Public assets (e.g., index.html)
โ”‚ โ””โ”€โ”€ package.json # Vue.js dependencies and scripts
โ”‚
โ”œโ”€โ”€ Dockerfile # Dockerfile for containerization
โ””โ”€โ”€ main.go # Go HTTP server code


Setting up Your Vue.js Project

First, you need to set up a Vue.js project. Make sure you have Node.js and Vue CLI installed. Open your terminal and run the following commands:

  1. Install Vue CLI globally:

    npm install -g @vue/cli
    
  2. Create a new Vue project named first-vue-app:

    vue create first-vue-app
    

    Choose the default preset when prompted.

  3. Navigate into the project directory:

    cd first-vue-app
    

Modifying the Vue.js Application

Now, let's modify the default Vue component to display "Hello World !!". Open src/App.vue in your favorite code editor and replace its content with the following code:

Vue.js Component
<template>
 <div id="app">
 <h1>Hello World !!</h1>
 </div>
</template>

<script>
export default {
 name: 'App',
};
</script>

<style>
#app {
 font-family: Avenir, Helvetica, Arial, sans-serif;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
</style>


Building the Vue.js Application

Before serving the application, you need to build it into static files. Run this command in your terminal:

npm run build


This will generate a dist directory containing the compiled files.

Creating a Go HTTP Server

Next, you'll create a Go server to serve these static files. In the root directory (outside the first-vue-app directory), create a file named main.go and add the following content:

Go
package main

import (
 "log"
 "net/http"
)

func main() {
 fs := http.FileServer(http.Dir("./first-vue-app/dist"))
 http.Handle("/", fs)

 log.Println("Listening on :8080...")
 err := http.ListenAndServe(":8080", nil)
 if err != nil {
 log.Fatal(err)
 }
}


This simple Go server serves the static files from the dist directory.

Creating a Dockerfile

To make deployment easier, let's containerize the application using Docker. Create a Dockerfile in the root directory with the following content:

Dockerfile
# Stage 1: Build Vue.js app
FROM node:16 AS build

WORKDIR /app

COPY first-vue-app/package*.json ./first-vue-app/
RUN cd first-vue-app && npm install
COPY first-vue-app /app/first-vue-app
RUN cd first-vue-app && npm run build

# Stage 2: Build Go server
FROM golang:1.20

WORKDIR /app
COPY main.go .
COPY --from=build /app/first-vue-app/dist /app/first-vue-app/dist
RUN go build -o server main.go

EXPOSE 8080
CMD ["./server"]


This Dockerfile uses multi-stage builds to first compile the Vue.js application and then set up a Go server to serve the built files.

Building and Running the Docker Container

Now, build the Docker image with the following command:

docker build -t my-vue-go-app .


Once the build is complete, run the container:

docker run -p 8080:8080 my-vue-go-app


Your Vue.js application should now be accessible at http://localhost:8080, displaying "Hello World !!".

Benefits of Serving Vue.js With Go

Performance and Efficiency

  • High concurrency: Go is known for efficiently handling many simultaneous connections, making it ideal for web servers.
  • Fast static file serving: Goโ€™s HTTP server serves static files quickly, providing a smooth user experience.

Simplified Architecture

  • Single deployment unit: Serving both the front-end and back-end from a single Go application simplifies the deployment process and reduces the complexity of managing multiple servers.
  • Reduced latency: Hosting the static assets and API endpoints on the same server can reduce latency, resulting in a more responsive application.

Conclusion

Serving a Vue.js application with a Go HTTP server combines the strengths of both technologies, providing a performant, efficient, and easy-to-maintain solution. This approach is particularly beneficial for projects requiring high concurrency, simplified deployment, and seamless integration between the front-end and back-end components.

Vue.js application Go (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Deploying a Scalable Golang Application on Kubernetes: A Practical Guide
  • MongoDB Change Streams and Go
  • Protecting Go Applications: Limiting the Number of Requests and Memory Consumption
  • Adding a Gas Station Map to a React and Go/Gin/Gorm Application

Partner Resources

ร—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: