{ // vercel.json
"builds": [{ "src": "index.js", "use": "@vercel/node" }],
"routes": [{ "src": "/(.*)", "dest": "/index.js" }]
}Best practice for RESTful API design is that path params are used to identify a specific resource or resources, while query parameters are used to sort/filter those resources.
http://localhost:5000/orders/?email=mr.es20x@gmail.com
// are same
http://localhost:5000/orders?email=mr.es20x@gmail.com- add
app.use(express.json());- this will convert the json into a js object
const postStudent = (name) => {
fetch("https://backend-integration-test.vercel.app/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name: name }),
})
.then((res) => res.json())
.then((data) => setStudents(data));
};- get the mongoDB_URI. It may looks like
mongodb+srv://sohanemon:<password>@<clusterName>.2gtvyou.mongodb.net/?retryWrites=true&w=majorityormongodb://localhost:8000/" - then set as a client
const { MongoClient } = require("mongodb");
// step: 1
const client = new MongoClient(process.env.MONGODB_URI);- retrieve the collection from database
try {
// step: 2
const collection = client.db("<myDbName").collection("<myCollectionName>");
// myCollectionName collection is inside the myDbName database
} finally {
}- operate different operations on collection variable now
const runMongo = async () => {
try {
const collection = client.db("users").collection("students");
const doc = {
name: "rafi",
};
// step: 3
collection.insertOne(doc);
} finally {
}
};make sure to run
runMongo()
as it is async then we can use
runMongo().catch(console.log)
- now send the fetched data from frontend to the server as
try {
const collection = client.db("users").collection("students");
app.post("/register", async (req, res) => {
const doc = req.body;
// step: 3
const result = await collection.insertOne(doc);
res.send(result);
});find()/findOnetakesqueryandoptionsas parameter
try {
const collection = client.db("users").collection("students");
app.get("/students", async (req, res) => {
// step: 3
const cursor = collection.find({});
// await is not required here bcoz find returns a cursor not a promise
// step: 4
const result = await cursor.toArray();
// .toArray() returns a promise to await is required
res.json(result);
});
}...findOnedoesn't return any cursor. So,awaitshould be used withfindOnefunction as it returns a promise- by hovering on any function, vs code shows what actually a function return like this. Anything after
:or=>determines what actually returning there.
app.get("/student/:_id", async (req, res) => {
const _id = req.params._id;
const result = await collection.findOne({ _id: ObjectId(_id) });
res.json(result);
});- Similar to other it also have
updateOneandupdateManyfunction
app.put("/update/:_id", async (req, res) => {
const _id = req.params._id;
const doc = req.body;
const result = await collection.updateOne(
{ _id: ObjectId(_id) },
{
$set: doc,
}
);
res.send(result);
});deleteOnefunction takes a query argument. Which describes about the items.- for deleting an item using _id one should pass using
ObjectIdfunction.
app.delete("/delete/:_id", async (req, res) => {
const _id = req.params._id;
// step: 4
const result = await collection.deleteOne({ _id: ObjectId(_id) });
res.send(result);
});