How many way we can create server for Http1 and Http2 node js

6/29/2023

#how to create server in node js using express #node js #server #hosting #node js multiple servers same

Go Back

How many way we can create server for Http1 and Http2 node js 

In this Article, we will learn how to create a simple Node.js web server and handle HTTP  and Http2 requests.
Node.js is an open-source and cross-platform JavaScript runtime environment. It is most  popular tool for almost any kind of project like web and static as well report generation UI .
Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.
 
To follow along with this tutorial - the reader will need the following:
A basic knowledge of JavaScript programming is essential.
Having basic  idea of Node.js installed on your machine.
 
Following are way to create  Server in node js 
 
#how to create server in node js using express #node js #server #hosting #node js multiple servers same

1) Creating Server using ‘http‘ Module:

 
Import http module: Import http module and store returned HTTP instance into a variable.
 
var express = require('express');
var path = require('path');
var netjet = require('netjet');
const root = path.join(__dirname, '/public')
const app = express()
 
app.set('views', path.join(__dirname, 'view'));  
app.set('view engine', 'ejs')
 
app.get("/", async (req, res) => {
  res.render("index")
})
 
app
  .use(netjet({
    cache: {
      max: 100
    }
  }))
  .use(express.static(root))
  .listen(3008);
 

2) Creating Server using ‘http2‘ Module:

 
Import http module: Import http module and store returned HTTP2 instance into a variable.
 
 
const http2Express = require('http2-express-bridge')
const http2 = require('http2')
const { readFileSync } = require('fs')
const express = require("express")
const fs = require("fs")
var path = require('path');
var cors= require('cors')
 
const app = http2Express(express)
const bodyParser = require("body-parser");
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
 
const PUBLIC_PATH = path.join(__dirname, '/public')
 
app.use(express.static("public"))
app.set('views', path.join(__dirname, 'view'));  
app.set('view engine', 'ejs')
 
const options = {
     key: fs.readFileSync("./server.key"),
    cert: fs.readFileSync("./server.crt")
};
 
app.get('/', function (req, res) {
  res.render('index')
})
 
 
const server = http2.createSecureServer(options,app)
server.listen(3000, () => {
        console.log(`listening on port 3000`)
})
 
 

3) Creating Server using ‘http2‘ Module:

 
Import http module: Import http module and store returned spdy instance into a variable.
 
const spdy = require("spdy")
const express = require("express")
const fs = require("fs")
const {promisify} = require("util")
var netjet = require('netjet');
var path = require('path');
const app = express()
app.use(netjet({
  cache:{max:100}
}))
app.use(express.static("public"))
app.set('views', path.join(__dirname, 'view'));  
app.set('view engine', 'ejs')
 
app.get("/", async (req, res) => {
res.render("index")
})
 
spdy.createServer(
  {
    key: fs.readFileSync("./server.key"),
    cert: fs.readFileSync("./server.crt"),
  spdy: {
    plain: false,
    protocols: [ 'h2', 'spdy/3.1','spdy/2','spdy/3', 'http/1.1' ],
    'x-forwarded-for': true
  }
  }
  ,
  app
 
).listen(3000, (err) => {
  if(err){
    throw new Error(err)
  }
  console.log("Listening on port 3000")
})
 

Conclusion

Here we learn about different ways in nodejs .
We import module like http , http2 and spdy  to create server.

Now, run your web server using node app.js. Visit http://localhost:3000 and you will see a message saying "Hello World".
Refer to the Introduction to Node.js for a more comprehensive guide to getting started with Node.js.
 
More Examples
See https://github.com/nodejs/examples for a list of Node.js examples that go beyond hello world.