diff --git a/server/getProduct.js b/server/getProduct.js new file mode 100644 index 0000000..112637b --- /dev/null +++ b/server/getProduct.js @@ -0,0 +1,11 @@ +const products = require('../products.json') + +const getProduct = (req, res) => { + const item = products.find(val => val.id === parseInt(req.params.id)); + if (!item) { + return res.status(500).send('Item not in list'); + } + res.status(200).send(item); +}; + +module.exports = getProduct \ No newline at end of file diff --git a/server/getProducts.js b/server/getProducts.js new file mode 100644 index 0000000..1d5e57a --- /dev/null +++ b/server/getProducts.js @@ -0,0 +1,13 @@ +const products = require('../products.json') + +const getProducts = (req, res) => { + if (req.query.price){ + const items = products.filter( + val => val.price >= parseInt(req.query.price) + ); + res.status(200).send(items) + } + res.status(200).send(products) +}; + +module.exports = getProducts \ No newline at end of file diff --git a/server/index.js b/server/index.js new file mode 100644 index 0000000..c9205af --- /dev/null +++ b/server/index.js @@ -0,0 +1,15 @@ +const express = require('express') +const getProducts = require('./getProducts') +const getProduct = require('./getProduct') + +const app = express(); + +const PORT = 3000 + +app.get('/api/products', getProducts) +app.get('/api/product/:id', getProduct); + +app.listen(PORT, () => { + console.log(`App listening on port ${PORT}!`); +}); +