Why Express js is Unopinionated? Why Express js in 2024?

Why Express js is Unopinionated? Why Express js in 2024?



ExpressJS is a lightweight and adaptable web application framework for Node.js, offering a strong set of features for developing web and mobile applications.
Known for its simplicity and ease of use, ExpressJS enables developers to create powerful APIs and handle HTTP requests with ease. Keep reading the blog, to learn more about it.

What is ExpressJS?

  • ExpressJS is a light-weight and flexible framework that is built on top of NodeJS.
  • It inherits all the features of NodeJS like: asynchronous and event-driven
  • programming and has it's own robust features which facilitate and ease the process of creating scalable and fundamentally strong web-applications.

Express Js is Unopinionated

  • What does this mean? This means that express doesn't enforce a specific way of structuring your application or using particular tools and libraries.
  • How does the unopinionated nature works? It provides a minimal set of features and allows developers the freedom to choose and integrate the modules and tools that best suit their needs.
  • What is the benefit? This allows flexibility and makes Express highly versatile and adaptable to a wide range of projects and coding styles.

Why should you choose ExpressJS?

Being flexible and robust are just the tip of the iceberg and Express is a lot more than what meets the eye. Below is a list of key features that will portray the usefulness of Express.
  • User Friendly and Easy to learn: Being JavaScript as it's programming language, it becomes easier for developers to learn and get started with express. Anyone who is comfortable in writing JavaScript can start learning express.
  • Better HTTP Handling: Express.js enhances HTTP handling by providing routing methods and support for HTTP verbs (GET, POST, PATCH,
  • DELETE), enabling more precise and efficient management of the request-response cycle.
  • Support for Middleware: Middleware is a reusable chunk of code used to intercept incoming requests for various purposes,
  • such as parsing data or handling authentication. Express allows us to incorporate middleware into our request-response cycle, providing greater control over our application.
  • Asynchronous Programming: Since express is built on top pf NodeJS,
  • it inherits the asynchronous nature and provides a more faster and seamless experience when building applications.
  • Static Asset Serving: We can also server static files straight from our server through dedicated directories.
  • These files can include images, stylesheets and even html files(static content only).

Setting up the Development Environment

Enough boasting about how good is Express. Let's get to creating stuff! To start working with express you'll need:
  • A Text Editor: we will be using VS Code for this tutorial.
  • Head over to Visual Studio's download page and download the suitable installer for Windows/MacOS/Linux.
  • NodeJS: We will also need to install NodeJS on our machine to work with express.
  • You can download it from their homepage.
  • Make sure to download the LTS(long term support) version to ensure a bug free experience.

Creating an Express Server

After installing these two software, open up a command prompt window in a directory of your choice and run the following commands:
  • Create a project directory and move into it
1mkdir express-tutorial
2cd express-tutorial
  • Install express module
1npm install express
  • This command will install express module locally into your project folder.
  • If you want to install express globally, you can prefix the name of the package with the -g flag like so:
1npm install -g express
  • Create the entry point for your server
For MacOS and Linux:
1touch server.js
For Windows:
1type nul > server.js
  • Open the file in VS Code
1code server.js

Initializing Express app

Paste the following code in your server.js file to create the server. The code is explained afterwards:
1var express = require("express");
2
3// Create an instance of the express module
4var app = express();
5
6// Define a port variable
7const port = 3000;
8
9// Start the app/server
10app.listen(port, () => {
11  console.log(`Server running on: http://localhost:${port}`)
12})

Explanation

  • var express = require("express"): This line of code imports the express module and assigns is to a variable named express, you can call it anything you like.
  • var app = express(): Here, we invoke the built-in express() method provided by the express module to create an app instance.
  • Any routes or middleware we want to use, we will call them on this instance of our server.
  • const port = 3000: This is just a simple declaration for a port number. This is the port where our server is going to run/reside.
  • app.listen(...): Lastly, we invoke the listen() method on our server's instance and pass 2 parameters: the port, and a callback function that runs when the server runs successfully.

Next Steps

Save the file and open VS Code's integrated terminal(Cmd+Shift+` for mac and Ctrl+` for windows).
Now, in the terminal, make sure you are in your project directory, if not then move into it by using:
1cd express-tutorial
replace express-tutorial with the name of your folder.
All that's left now, is to start our server. In your terminal execute the following command:
1node server.js
This command executes the server.js file and starts our server in our terminal. If everything was set up correctly, you should see something like this in your terminal window:
image
Congratulations!! You just setup and ran your very own express server!

Conclusion

In this article, we learned about:
  • What is Express JS?
  • Why should we opt for Express JS?
  • Setting up our development environment for express programming
  • Created a basic express server
In the next blog, we will talk about how express handles request-response cycles and routing.
Stay tuned for more express tutorials and I'll catch you in the next one! 👋