0
0
Expressframework~5 mins

What is Express

Choose your learning style9 modes available
Introduction

Express helps you build web servers easily. It makes handling web requests simple and fast.

You want to create a website backend to serve pages or data.
You need to build an API for a mobile app or frontend.
You want to handle user input from forms or URLs.
You want to manage routes for different web pages.
You want to quickly prototype a web service.
Syntax
Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

express() creates an app to handle web requests.

app.get(path, handler) listens for GET requests on a path.

Examples
Sends 'Welcome!' when someone visits the home page.
Express
app.get('/', (req, res) => {
  res.send('Welcome!');
});
Handles form data sent with POST method at '/submit'.
Express
app.post('/submit', (req, res) => {
  res.send('Form submitted');
});
Starts the server on port 4000 and logs a message.
Express
app.listen(4000, () => {
  console.log('Server on port 4000');
});
Sample Program

This program creates a simple web server. When you visit http://localhost:3000, it shows 'Hello from Express!'. The console shows a message that the server is running.

Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Express!');
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
OutputSuccess
Important Notes

Express is built on top of Node.js to simplify web server creation.

Use app.use(express.json()) to handle JSON data in requests.

Always listen on a port to start the server.

Summary

Express makes building web servers easy and fast.

It handles routes and requests with simple code.

Great for websites, APIs, and quick prototypes.