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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Protecting Go Applications: Limiting the Number of Requests and Memory Consumption
  • Adding a Gas Station Map to a React and Go/Gin/Gorm Application
  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • Go Application Vulnerability Cheatsheet

Trending

  • How Clojure Shapes Teams and Products
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Scalability 101: How to Build, Measure, and Improve It
  • Virtual Threads: A Game-Changer for Concurrency
  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
4.8K 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

  • Protecting Go Applications: Limiting the Number of Requests and Memory Consumption
  • Adding a Gas Station Map to a React and Go/Gin/Gorm Application
  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • Go Application Vulnerability Cheatsheet

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!