Chapter 3Node.js & Express~1 min read
Express.js — पहिला Server
Web Server बनवायला शिका
Express.js हे Node.js साठी सर्वाधिक popular web framework आहे. HTTP server बनवणे, routes define करणे, middleware वापरणे — हे सगळं Express मुळे खूप सोपं होतं.
Express install करा
bash
npm init -y
npm install express
npm install nodemon --save-devपहिला Express server
javascript
// app.js
const express = require('express');
const app = express();
const PORT = 3000;
// Middleware — JSON body parse करा
app.use(express.json());
// Routes
app.get('/', (req, res) => {
res.send('नमस्कार! हा माझा पहिला Express server आहे!');
});
app.get('/about', (req, res) => {
res.json({ message: 'Tech Tatya API', version: '1.0' });
});
// Server start करा
app.listen(PORT, () => {
console.log(`Server चालू आहे: http://localhost:${PORT}`);
});Server run करा
bash
# nodemon — file save केल्यावर auto restart
npx nodemon app.js
# Browser मध्ये जा: http://localhost:3000HTTP Methods
- ▸GET — data मागवा (read)
- ▸POST — नवीन data पाठवा (create)
- ▸PUT/PATCH — existing data update करा
- ▸DELETE — data delete करा
✅ Key Points — लक्षात ठेवा
- ▸express() — app instance बनवतो
- ▸app.get/post/put/delete — routes define
- ▸req = request object, res = response object
- ▸res.json() — JSON response पाठवा
- ▸app.listen(PORT) — server start
0/8 chapters पूर्ण