50% found this document useful (2 votes)
8K views

Wings1 T1 Nodejs APIs (62637)

The document provides instructions for setting up a Node.js API with Express for selling and buying products. It includes code to create routes for getting, posting, patching, and deleting product data from a MongoDB database. Developers are instructed to copy the code snippets into the appropriate files to create the API endpoints and configure Express, body parsing, and error handling middleware. The API can then be installed, run, and tested.

Uploaded by

Pushpendra Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
8K views

Wings1 T1 Nodejs APIs (62637)

The document provides instructions for setting up a Node.js API with Express for selling and buying products. It includes code to create routes for getting, posting, patching, and deleting product data from a MongoDB database. Developers are instructed to copy the code snippets into the appropriate files to create the API endpoints and configure Express, body parsing, and error handling middleware. The API can then be installed, run, and tested.

Uploaded by

Pushpendra Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Join our channel if you haven’t joined yet https://t.

me/fresco_milestone
( @fresco_milestone )

Wings1 T1 Mini-project - Nodejs APIs

Open Project folder (you can find in desktop) in VS Code application

Type below code in “/Desktop/Projects/wings.../Nodejs/src/routers/sellandBuy js”


file:

const e = require("express");

const express = require("express");

const SellBuy = require("../mongoose/models/sellBuy")


const router = express.Router()

router.get("/", async (req, res) => {


try {
if (req.query.product) {
const data = await SellBuy.find({ productName: req.query.product })
res.status(200) .json(data)
} else if (req.query.sortBy) {
let list = req.query.sortBy
let ord = -1;
if (list.charAt(@) == "1") {
ord = 1
}
if (list.includes("S")) {
const data = await SellBuy.find().sort({ soldPrice: ord })
res.status(20@).json(data)
} else {
const data = await SellBuy.find().sort({ costPrice: ord })
res.status(209).json(data)
}
} else {
const data = await SellBuy.find()
res.status(290) .json(data)
}
} catch (err) {
res.status(200).json(data)

}
})

router.post("/", async (req, res, next) => {


const data = new SellBuy(req. body)
try {
if (data.productName.length < 4) {
const err = new Error("product name should have minimum of four
characters")
Join our channel if you haven’t joined yet https://t.me/fresco_milestone
( @fresco_milestone )

err.status = 400
next(err)
} else if (data.costPrice <= 0) {
const err = new Error("cost price value cannot be zero or negative
value")
err.status = 400
next(err)

} else if (data.soldPrice <= @) {


const err = new Error("sold price value cannot be zero or negative

value")
err.status = 400
next(err)
} else {
await data.save();
res.status(201).json({ message: "Product Added" })
}

} catch (err) {
res.status(200@) .json(data)

})

router.patch("/:id", async (req, res, next) => {

try {
const data = req.body.soldPrice;

if (data <= @) {
const err = new Error("sold price value cannot be zero or negative
value")
err.status = 460
next (err)
} else {
res.status(20@).json({ message: "Updated Successfully" })

}
} catch (err) {

res.status(20@) .json(data)

})

router.delete("/:id", async (req, res) => {


try {
let id = req.params.id
if (id === undefined || id === '' || id === null) {
Join our channel if you haven’t joined yet https://t.me/fresco_milestone
( @fresco_milestone )

res.status(40@) .json(data)

}
const data = await SellBuy.findByIdAndDelete(id)

res.status(200).json({ message: "Deleted successfully" })


} catch (err) {
res.status(400).json({ error: err })

})

Type below code in “/Desktop/Projects/wings.../ Nodejs /src/app.js” file:

const express = require("express");

const sellAndBuyRouter = require("./routers/sellAndBuy") ;


const bodyParser = require("body-parser");
require("./mongoose/connect_db/mongoose" );

const app = express();


app.use(express.json());
app.use(bodyParser.json());
app.use((req, res, next) => {
res. header("Access-Control-Allow-Origin", "*");
res. header("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res. header("Access-Control-Allow-Methods", "GET,DELETE,PATCH");
return res.status(200).json({});

}
next();

})3

app.use('/sellProduct', sellAndBuyRouter) ;

app.use((error, req, res, next) => {


res.status(error.status || 500)
res.json({ error: error.message })

})

module.exports = app;

Install project dependencies by using command in the terminal and


to run the application by using

To test the application

You might also like