DEV Community

Pierangelo
Pierangelo

Posted on

2 1

nodejs vs golang server web

After the comparison between NodeJS and GO about how to use MongoDB, now will see how programming a very simple and basic web server with this two technologies.

Golang:

file main.go

package main

import "net/http"

func homePage(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Welcome to my Home Page"))
}
func main() {
    http.HandleFunc("/", homePage)
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}
Enter fullscreen mode Exit fullscreen mode

Run server:

go run main.go

Enter fullscreen mode Exit fullscreen mode

Should be run on:
http://127.0.0.1:8080

NodeJS

file app.js

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to my Home Page');
});

server.listen(port, hostname, () =>  {
    console.log(`Server running at http://${hostname}:${port}/`);
});
Enter fullscreen mode Exit fullscreen mode

run server:

node app.js
Enter fullscreen mode Exit fullscreen mode

Should be run on:
http://127.0.0.1:3000

So as you see, with both you can get a running web server with few lines of code.

Have a Nice Day!

Respository:

Video Tutorial:

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn 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

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay