TGViewer
Web Development Web Development @web_dev_bds Β· 4.43K subscribers
Post #727 632
βœ… Express.js Basics You Should Know πŸš€πŸ“¦

Express.js is a fast, minimal, and flexible Node.js web framework used to build APIs and web apps.

1️⃣ What is Express.js? πŸ—οΈ
A lightweight framework on top of Node.js that simplifies routing, middleware, request handling, and more.

2️⃣ Install Express: πŸ“¦
npm init -y
npm install express


3️⃣ Basic Server Setup: πŸš€
const express = require('express');
const app = express();

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

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


4️⃣ Handling Different Routes: πŸ—ΊοΈ
app.get('/about', (req, res) => res.send('About Page'));
app.post('/submit', (req, res) => res.send('Form submitted'));


5️⃣ Middleware: βš™οΈ
Functions that run before a request reaches the route handler.
app.use(express.json()); // Example: Parse JSON body


6️⃣ Route Parameters & Query Strings: ❓
app.get('/user/:id', (req, res) => {
  res.send(`User ID: ${req.params.id}`); // Access route parameter
});

app.get('/search', (req, res) =>
  res.send(`You searched for: ${req.query.q}`); // Access query string
);


7️⃣ Serving Static Files: πŸ“
app.use(express.static('public')); // Serves files from the 'public' directory


8️⃣ Sending JSON Response: πŸ“Š
app.get('/api', (req, res) => {
  res.json({ message: 'Hello API' }); // Sends JSON response
});


9️⃣ Error Handling: ⚠️
app.use((err, req, res, next) => {
  console.error(err.stack); // Log the error for debugging
  res.status(500).send('Something broke!'); // Send a generic error response
});


πŸ”Ÿ Real Projects You Can Build: πŸ“
- RESTful APIs
- To-Do or Notes app backend
- Auth system (JWT)
- Blog backend with MongoDB

πŸ’‘ Tip: Master your tools to boost efficiency and build better web apps, faster.

πŸ’¬ Tap ❀️ for more!
  • ❀ 3
More from @web_dev_bds
  1. Oct 16, 2026πŸ“•Random React Tip: If you want to quickly log things from JSX (we've all been there), you…
  2. Oct 15, 2026Websites to Learn Every Technology
  3. Oct 13, 2026Docker vs Kubernetes
  4. Oct 12, 2026Post #1054
  5. Oct 11, 2026CSS Tip Stop centring tooltips with πšπš›πšŠπš—πšœπš•πšŠπšπšŽ: -𝟻𝟢%. near the edge of the scree…
  6. Oct 9, 2026JavaScript CheatSheet
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’