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:

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up