,

Express js और Node js Rest API tutorial हिंदी में 2024, API को डालें, MongoDB #nodejstutorial #restapi

Posted by

<!DOCTYPE html>

Express.js, Node.js Rest API Tutorial in Hindi 2024

Express.js, Node.js Rest API Tutorial in Hindi 2024

Is tutorial mein hum Express.js aur Node.js ka istemal karke ek Rest API banayenge.

Prerequisites:

1. Node.js aur MongoDB install kiya hua ho.

2. Text editor jaise ki Visual Studio Code ka istemal karein.

Step 1: Express.js Setup

Sabse pehle terminal mein “npm install express” command run karein.

Step 2: Project Folder create karein

Apne project ke liye ek naya folder create karein aur usmein “app.js” file create karein.

Step 3: Express.js server create karein

App.js file mein neeche di gayi code add karein:

 
 
const express = require('express'); 
const app = express(); 
const PORT = 3000; 

app.get('/', (req, res) => { 
res.send('Welcome to the Express.js server!'); 
}); 

app.listen(PORT, () => { 
console.log(`Server is running on http://localhost:${PORT}`); 
}); 
 

Step 4: Server run karein

Terminal mein “node app.js” command run karein aur browser mein http://localhost:3000/ open karein.

Step 5: MongoDB Setup

Terminal mein “npm install mongoose” command run karein. Mongoose MongoDB ko Express mein integrate karta hai.

Step 6: Connection create karein

App.js file mein neeche di gayi code add karein:

 
 
const mongoose = require('mongoose'); 

mongoose.connect('mongodb://localhost:27017/myDB', { 
useNewUrlParser: true, 
useUnifiedTopology: true, 
}); 
 

Step 7: Schema create karein

App.js file mein neeche di gayi code add karein:

 
 
const mongoose = require('mongoose'); 

const userSchema = new mongoose.Schema({ 
name: { 
type: String, 
required: true, 
}, 
age: { 
type: Number, 
required: true, 
}, 
}); 

module.exports = mongoose.model('User', userSchema); 
 

Step 8: Insert API create karein

App.js file mein neeche di gayi code add karein:

 
 
app.post('/users', (req, res) => { 
const newUser = new User({ 
name: req.body.name, 
age: req.body.age, 
}); 

newUser.save((err, result) => { 
if (err) { 
res.send(err); 
} else { 
res.send(result); 
} 
}); 
}); 
 

Step 9: Server restart karein

Terminal mein “node app.js” command run karein. Postman ya kisi bhi API testing tool se POST request bhejkar user insert kar sakte hain.

Conclusion:

Is tutorial mein humne Express.js, Node.js aur MongoDB ka istemal karke ek Rest API banaya hai. Umeed hai ki ye tutorial aapke liye helpful hoga.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SunilPatel-ls2gy
3 months ago

sir aapne bhoot aacchha samjhya aapne thakyou sir

@musicmasti1455
3 months ago

Nice content

@Kidsentertainment701
3 months ago

Bhut accha samjhaya apne