0% found this document useful (0 votes)
19 views2 pages

Padin API

Uploaded by

padin.jc
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
0% found this document useful (0 votes)
19 views2 pages

Padin API

Uploaded by

padin.jc
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/ 2

const express = require('express')

const mongoose = require('mongoose')


const Product = require('./models/productModel')
const app = express()

app.use(express.json())
app.use(express.urlencoded({extended: false}))

//routes

app.get('/', (req, res) => {


res.send('Hello NODE API')
})

app.get('/blog', (req, res) => {


res.send('Hello Sir Jasper, Our Group Name is HELLO PHILIPPINES')
})

app.get('/products', async(req, res) => {


try {
const products = await Product.find({});
res.status(200).json(products);
} catch (error) {
res.status(500).json({message: error.message})
}
})

app.get('/products/:id', async(req, res) =>{


try {
const {id} = req.params;
const product = await Product.findById(id);
res.status(200).json(product);
} catch (error) {
res.status(500).json({message: error.message})
}
})

app.post('/products', async(req, res) => {


try {
const product = await Product.create(req.body)
res.status(200).json(product);

} catch (error) {
console.log(error.message);
res.status(500).json({message: error.message})
}
})

// update a product
app.put('/products/:id', async(req, res) => {
try {
const {id} = req.params;
const product = await Product.findByIdAndUpdate(id, req.body);
// we cannot find any product in database
if(!product){
return res.status(404).json({message: `cannot find any product with ID
${id}`})
}
const updatedProduct = await Product.findById(id);
res.status(200).json(updatedProduct);

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

// delete a product

app.delete('/products/:id', async(req, res) =>{


try {
const {id} = req.params;
const product = await Product.findByIdAndDelete(id);
if(!product){
return res.status(404).json({message: `cannot find any product with ID
${id}`})
}
res.status(200).json(product);

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

mongoose.set("strictQuery", false)
mongoose.
connect('mongodb+srv://jomatyangelo:<padinrey123456>@cluster0.yoaqmwm.mongodb.net/?
retryWrites=true&w=majority')
.then(() => {
console.log('connected to MongoDB')
app.listen(3000, ()=> {
console.log(`Node API app is running on port 3000`)
});
}).catch((error) => {
console.log(error)
})

You might also like