Skip to content

Commit

Permalink
Merge pull request #23 from vinicius77/security-updates
Browse files Browse the repository at this point in the history
Security updates
  • Loading branch information
vinicius77 authored Feb 19, 2022
2 parents 8bbde39 + dabe9d0 commit 57767f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
31 changes: 31 additions & 0 deletions backend/controllers/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { v4: uuidv4 } = require('uuid');

const DUMMY_PRODUCTS = []; //in-memory storage data

const getProducts = async (req, res) => {
res.status(200).json({ products: DUMMY_PRODUCTS });
};

const createProduct = async (req, res) => {
const { title, price } = req.body;

if (!title || title.trim().length === 0 || !price || price <= 0) {
return res.status(422).json({
message: 'Invalid input, please enter a valid title and/or price.',
});
}

const createdProduct = {
id: uuidv4(),
title,
price,
};

DUMMY_PRODUCTS.push(createdProduct);

res.status(201).json({ message: 'Created New Product', product: createdProduct });
};
module.exports = {
createProduct,
getProducts,
};
8 changes: 8 additions & 0 deletions backend/routes/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();

const { createProduct, getProducts } = require('../controllers/products');

router.route('/').get(getProducts).post(createProduct);

module.exports = router;
28 changes: 2 additions & 26 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
const PORT = process.env.PORT || 5000;

const DUMMY_PRODUCTS = []; //in-memory storage data

app.use(express.json()); // Now express.js has the body-parser embebbed on it
app.use(express.urlencoded({ extended: false }));

// CORS Headers => Required for cross-origin- cross-server communication
app.use((req, res, next) => {
Expand All @@ -18,28 +16,6 @@ app.use((req, res, next) => {
next();
});

app.get('/products', (req, res, next) => {
res.status(200).json({ products: DUMMY_PRODUCTS });
});

app.post('/product', (req, res, next) => {
const { title, price } = req.body;

if (!title || title.trim().length === 0 || !price || price <= 0) {
return res.status(422).json({
message: 'Invalid input, please enter a valid title and/or price.',
});
}

const createdProduct = {
id: uuidv4(),
title,
price,
};

DUMMY_PRODUCTS.push(createdProduct);

res.status(201).json({ message: 'Created New Product', product: createdProduct });
});
app.use('/products', require('./routes/products'));

app.listen(PORT, () => console.log(`Server Listening on port ${PORT}`)); //Run node + express server on port 5000
2 changes: 1 addition & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function App() {
price: +productPrice,
};
let hasError = false;
const response = await fetch('http://localhost:5000/product', {
const response = await fetch('http://localhost:5000/products', {
method: 'POST',
body: JSON.stringify(newProduct),
headers: {
Expand Down

0 comments on commit 57767f0

Please sign in to comment.