DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Trending

  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  • A System Cannot Protect What It Does Not Understand
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2
  • Detecting Plan Regression in SQL Server Using Query Store
  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 
Bhanuprakash Jirra user avatar
Bhanuprakash Jirra
·
May. 24, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
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.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook