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 Gola…


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi

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
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}`)
})

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"))
}

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?


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi


Print Share Comment Cite Upload Translate Updates
APA

Abayomi Ogunnusi | Sciencx (2022-03-17T07:14:22+00:00) Start a server: Node Vs Golang. Retrieved from https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/

MLA
" » Start a server: Node Vs Golang." Abayomi Ogunnusi | Sciencx - Thursday March 17, 2022, https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/
HARVARD
Abayomi Ogunnusi | Sciencx Thursday March 17, 2022 » Start a server: Node Vs Golang., viewed ,<https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/>
VANCOUVER
Abayomi Ogunnusi | Sciencx - » Start a server: Node Vs Golang. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/
CHICAGO
" » Start a server: Node Vs Golang." Abayomi Ogunnusi | Sciencx - Accessed . https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/
IEEE
" » Start a server: Node Vs Golang." Abayomi Ogunnusi | Sciencx [Online]. Available: https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/. [Accessed: ]
rf:citation
» Start a server: Node Vs Golang | Abayomi Ogunnusi | Sciencx | https://www.scien.cx/2022/03/17/start-a-server-node-vs-golang/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.