Express.js Tutorial
Express.js is a minimal and flexiable backend web application framework for Node.js. It was created by TJ Holowaychuk in 2010. It is designed for building API's, single-page applications, or full-stack web applications. Express.js is to handle HTTP requests and responses.
Key Features of Express.js
- Middleware Support : Middleware functions are executed during the lifecycle of a request-response cycle.
- Routing : Expres.jss provides robust routing for managing different URL endpoints
- Template Engines : Express.js provides a variety of template engines like EJS, Pug, or Handlebars to render HTML pages.
- Error Handling : Express.js provides robust error-handling features that allow developers handle errors and respond to errors effectively.
- API Development : A lightweight framework for building RESTful APIs and web services.
Let's look at a basic Express.js app
index.js
const express = require('express'); const app = express(); const PORT = 5000; // Define a route app.get('/', (req, res) => { res.send('Welcome to Express.js!'); }); // Start the server and listen on the specified port app.listen(PORT, () => { console.log(`Server is running on http:// localhost:${PORT}`); });
Run the server using the command given below.
D:\my-app>node index.js
Why Use Express.js
- Fast and Lightweight
- Flexible and Minimalistic
- Middleware Support
- Robust Routing
- Speed and Simplicity
- Extensible Ecosystem
- Compatibility
Prerequisites to Learn Express.js
- JavaScript
- Node.js Fundamentals
- NPM
- Understanding of HTTP & REST
- Familiarity with JSON
- Command Line Basics