DEV Community

Cover image for Start a server: Node Vs Golang
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

3 3

Start a server: Node Vs Golang

Disclaimer: The purpose of this post is not to compare the two programs; rather, it is to demonstrate how to start the server.

Today, we'll look at how to start our server in Node.js with the express framework and Nodemon, as well as in Golang with the fiber framework and air.

start

Nodejs

Initialize your project

npm init -y

Install Pacakages

npm i express and npm i -D nodemon

Start server
node index
Enter fullscreen mode Exit fullscreen mode
const express = require("express")
const app  = express()

const port = process.env.PORT || 4546

app.get("/", (req,res)=>{
  res.send("Home page")
})
app.listen(port, ()=>{
   console.log(`app is running on port ${port}`)
})
Enter fullscreen mode Exit fullscreen mode

Image description


Golang

Initialize your project

go mod init "github.com/drsimplegraffit/fibre-api"

Install Pacakages

go get "gorm.io/gorm"
go get "github.com/gofiber/fiber/v2"

Start server
package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

func welcome(c *fiber.Ctx) error {
    return c.SendString("Welcome")
}

func main() {
    app := fiber.New()

    app.Get("/api", welcome)

    log.Fatal(app.Listen(":3002"))
}

Enter fullscreen mode Exit fullscreen mode
Run go server
## Method 1

go run main.go

Image description

## Method 2: with hot reload

Install the air packagehere

To install:
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

Run: air

Result:
Image description

Discuss

What other frameworks do you use for Golang and Nodejs besides fiber and Express?

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay