Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Nodejs controller

const products = require('../data.js')

const getProducts = ((req, res) => {
    res.json(products)
})

const getProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const product = products.find(product => product.id === id)

        if (!product) {
        return res.status(404).send('Product not found')
    }
    res.json(product)
})

const createProduct = ((req, res) => {
    const newProduct = {
        id: products.length + 1,
        name: req.body.name,
        price: req.body.price
    }
    products.push(newProduct)
    res.status(201).json(newProduct)
})

const updateProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const index = products.findIndex(product => product.id === id)
    const updatedProduct = {
        id: products[index].id,
        name: req.body.name,
        price: req.body.price
    }

    products[index] = updatedProduct
    res.status(200).json('Product updated')
})

const deleteProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const index = products.findIndex(product => product.id === id)
    products.splice(index,1)
    res.status(200).json('Product deleted')
})

module.exports = {
    getProducts,
    getProduct,
    createProduct,
    updateProduct,
    deleteProduct
}
Comment

NodeJS Controller

exports.findOne = (req, res) => {
  const id = req.params.id;

  Tutorial.findByPk(id)
    .then(data => {
      if (data) {
        res.send(data);
      } else {
        res.status(404).send({
          message: `Cannot find Tutorial with id=${id}.`
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Error retrieving Tutorial with id=" + id
      });
    });
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: props history 
Javascript :: how to take last element of array javascript 
Javascript :: AJAX GET Requests 
Javascript :: empty array 
Javascript :: js 2d array includes 
Javascript :: html form action javascript method 
Javascript :: vue create component 
Javascript :: react-datepicker 
Javascript :: build angular project 
Javascript :: redux workflow 
Javascript :: validate decimal number with 6 decimal digits javascript 
Javascript :: jq cheat sheet 
Javascript :: Requiring express 
Javascript :: how to count characters 
Javascript :: react calendar 
Javascript :: usereducer react 
Javascript :: express api 
Javascript :: sumo multiselect 
Javascript :: debounce polyfill 
Javascript :: delete class through the dom 
Javascript :: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms 
Javascript :: code for random dom background image change 
Javascript :: how to filter on a hidden column datatables 
Javascript :: how to acces db knex 
Javascript :: scratch addons 
Javascript :: force light theme in react native 
Javascript :: check if item is already registered in angular angularfire site:stackoverflow.com 
Javascript :: namespace react has no export member FC 
Javascript :: wordpress apostrophe problem in javascript 
Javascript :: datatables pass headers on request 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =