Is it necessary the express.js framework to develop nodeJS web application

Viewed 1338

I am trying to develop the simple web application by using HTML, NodeJS, and Postgres. is it possible to develop the web application without using ExpressJS. Please let me know how to do the basic CURD operations by using NodeJs, Javascript, and Postgres without using the ExpressJS framework. Thanks in advance.

2 Answers

It is not necessary to use Express. Express is just a library that uses Node-Functions to make it easier for you to implement a Webserver.

There are plenty alternatives out there: A small overview of the 'best'

For the CURD Operations see the documentation of the Frameworks.

Yes, you can skip express But It ain't gonna be easy.

But there is something else :

is it possible to develop the web application without using ExpressJS

It can mean,

  • you either use other frameworks, or
  • Node.js web app from scratch

In the first case, you have options to choose from. There are MVC frameworks like hapi, Flicker.js etc, REST API frameworks like restify, loopback etc

In the second case, you are faced with the job of writing custom implementations of many features provided by express.

I'm continuing Assuming you meant the second case...

For creating the HTTP server, you can use node's HTTP Module

var http = require('http');

http.createServer(function (req, res) {
  res.write('Hello World!'); 
  res.end(); 
}).listen(8080); 

This method makes it a pain to write a REST API

req.url; //the request URL
if(url == '/users/create')
{
    //do this
}
else if(url == '/users/details')
{
    //do that
}

To connect to postgres, you have to use node-postgres (pg) anyways (pg documentation)

const { Client } = require('pg')
const client = new Client()

await client.connect()

const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()

As for the CRUD operations, apart from their API implementation, the code is going to be almost the same. So you can refer to the tutorials that use express.

https://mherman.org/blog/postgresql-and-nodejs/

Related