Node.js : Day 5
15 days Challenge

WHAT IS EXPRESS, AND WHY USE IT?
👉 Express is a minimal node.js framework, a higher level of abstraction;
👉 Express contains a very robust set of features: complex routing, easier handling of requests and responses, middleware, server-side rendering, etc.
👉 Express allows for rapid development of node.js applications: we don’t have to re-invent the wheel;
👉 Express makes it easier to organize our application into the MVC architecture.
Setting Up Express
Step 1. Initialize a node project
Before you can use Express, you need to initialize a Node.js project. This involves creating a package.json
file where all project dependencies and metadata are stored.
Run the following command in your terminal to initialize the project:
$ npm init
Step 2. Install express
$ npm i express
Step 3. Writing our First Express Server
const express = require("express");
// creating an express server
const app = express();
// define a simple route with get request
app.get("/", (req, res) => {
res.send("Hello From the server!!!");
});
// define a route with post request
app.get("/", (req, res) => {
res.send("This is post request!!!.");
});
// started server and server is listening on port 3000
app.listen(3000, () => {
console.log("App is running on port 3000");
});
Pros of Express
- Minimal and Flexible: Express is lightweight and doesn’t force you into a specific way of coding.
- Fast: Built for high performance and can handle many requests at once.
- Large Ecosystem: Many ready-to-use libraries (middleware) to add features easily.
- Routing: Easily define routes to handle different types of requests (GET, POST, etc.).
- Asynchronous: Can handle many operations at once without blocking other tasks.
- Scalable: Great for building large apps that can grow.
- Template Support: Works with template engines like EJS for dynamic web pages.
- Active Community: Lots of documentation and support from developers.
Cons of Express
- Minimal Structure: No predefined way to organize big apps, can lead to inconsistent code.
- No Built-In ORM: Doesn’t have a built-in tool for database interaction; you need a third-party tool.
- Middleware Complexity: Managing many middleware functions can get tricky in big apps.
- Callbacks: Uses callbacks, which can be hard to manage without promises or async/await.
- Limited Built-In Features: Doesn’t offer as many out-of-the-box features as other frameworks.
- No Official CLI: Unlike other frameworks, it doesn’t come with a built-in command-line tool to automate tasks.
- Not Ideal for Real-Time: Express doesn’t handle real-time features as well as other tools like Socket.io.
The framework is used by many leading companies such as Uber, Netflix, Airbnb, and IBM, making it a trusted choice for both small-scale applications and large enterprise systems.
Thanks…