NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

routes/product.js
const Product = require('../../models/product')

module.exports = function (router){
//get
router.get('/product', function(req,res){
Product.find({}, (err,product)=>{

if(err){
res.json({success:false,message:err}); // return err msg
}else{
//
if(!product){
res.json({success:false, message: 'No product found'});//return err msg
}else{
res.json(product);
// res.json({success:true, product: product}); // return success and product array
}
}
})
})
//get specific
router.get('/product/:_id', async(req,res)=>{
try{
const post = await Product.findById(req.params._id);
res.json(post);
}
catch(err){
res.json({message : err})
}
})



//post : get new meeting note document..
router.post('/product', function(req , res){
let note = new Product(req.body)
note.save(function (err , note){
if(err){
return res.status(400).json(err)
}
res.status(200).json(note)
})
})


router.put('/updateProduct/:_id', (req, res)=>{
//check if id is provided
if(!req.body._id){
res.json({success: false, message: "No product is provided"}); // return error message
}else{
//check if id exicts in db
Product.findOne({_id:req.body._id}, (err,product)=>{
//check if id is valid id
if(err){
res.json({success: false , message: 'Not a valid product id'});

}else{
product.productID = req.body.productID;
product.prodName = req.body.prodName;
product.brand = req.body.brand;
product.price = req.body.price;
product.manfDate = req.body.manfDate;
product.expDate = req.body.expDate;
product.save((err)=>{
if(err){
res.json({success : false, message: err}); //return err msg
}else{
res.json({success: true, message : 'Poduct Updated!'}); // return success message
}
});
}
});
}
});

router.delete('/deleteProduct/:productID',(req,res)=>{
//check id if was provided in parameters
if(!req.params.productID){
res.json({ success: false , message:'No id provided'}); //return error msg
}else{
//check id is found in db
Product.findOne({productID: req.params.productID},(err,product)=>{
//check if error was found
if(err){
res.json({success:false, message:'Invalid ID'});//return err msg
}else{
//remove the product from db
product.remove((err)=>{
if(err){
res.json({success : false, message:err});//return err msg
}else{
res.json({success:true, message:'Product deleted'});// return success msg
}
});
}
});
}
});
}
index.js
const express = require('express')
const router = express.Router()
//api/index.js

require('./routes/product')(router)

module.exports = router
models/product.js
const mongoose = require('mongoose')

const productSchema = new mongoose.Schema({
productID : {type : Number},
prodName : {type : String},
brand : {type : String},
price : {type : Number},
manfDate : {type : Date},
expDate : {type : Date},
// Image: {type:String}

})

module.exports = mongoose.model('Product', productSchema)
app.js:-
const express = require ('express')
const app = express()
const api = require('./api')
const morgan = require('morgan')//logger
const bodyParser = require ('body-parser')
const cors = require('cors')

app.set('port' ,(process.env.PORT || 8080))

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

app.use(cors())

app.use('/api', api)
app.use(express.static('static'))

app.use(morgan('dev'))
app.use(function(req,res){
const err = new Error('not found')
err.status = 400
res.json(err)
})

//MongoDB connection
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/virtualproduts', {useNewUrlParser: true})

const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function(){
console.log('connected to mongo')

app.listen(app.get('port'), function(){
console.log('API Server Listening on port' + app.get('port') + '!')
})
})
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.