Commit 2ddbc097 authored by Vadim's avatar Vadim

конец занятия №75

parent 8c708bd8
[{"title":"Новый продукт2","price":100,"description":"Product description"},{"title":"Новый продукт3","price":100,"description":"Product description"}]
\ No newline at end of file
[{"title":"Новый продукт2","price":100,"description":"Product description","_id":"po07wU69_bGgD9lJaGBoB"},{"title":"Новый продукт3","price":100,"description":"Product description","_id":"po07wU69_bGgD9lJaGBoA"},{"title":"Название продукта3","price":100,"description":"Описание продукта3","_id":"po07wU69_bGgD9lJaGBoR"}]
\ No newline at end of file
const fs = require('fs');
const fileName = './db/db.json';
const fileName = './app/db/db.json';
let data = [];
module.exports = {
......@@ -9,6 +9,7 @@ module.exports = {
const fileContents = fs.readFileSync(fileName, 'utf-8');
data = JSON.parse(fileContents);
}catch (e) {
console.error(e);
data = []
}
},
......
const express = require('express');
const router = express.Router();
const db = require('./db/fileDb');
const {nanoid} = require('nanoid')
db.init();
router.get('/', (req, res) => {
res.send('List of products will be here')
const products = db.getItems();
res.send(products)
});
router.get('/:id', (req, res) => {
......@@ -14,8 +16,12 @@ router.get('/:id', (req, res) => {
router.post('/', (req, res) => {
console.log('Create product', req.body);
db.addItem(req.body);
res.send('Create product will be here')
const newProduct = {
...req.body,
_id: nanoid()
};
db.addItem(newProduct);
res.send(newProduct)
});
module.exports = router;
\ No newline at end of file
......@@ -330,6 +330,15 @@
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"requires": {
"object-assign": "^4",
"vary": "^1"
}
},
"crypto-random-string": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
......@@ -855,6 +864,11 @@
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
},
"nanoid": {
"version": "3.1.18",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.18.tgz",
"integrity": "sha512-rndlDjbbHbcV3xi+R2fpJ+PbGMdfBxz5v1fATIQFq0DP64FsicQdwnKLy47K4kZHdRpmQXtz24eGsxQqamzYTA=="
},
"negotiator": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
......@@ -899,6 +913,11 @@
"integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==",
"dev": true
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
......
......@@ -10,7 +10,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
"cors": "^2.8.5",
"express": "^4.17.1",
"nanoid": "^3.1.18"
},
"devDependencies": {
"nodemon": "^2.0.6"
......
const express = require('express');
const products = require('./app/products');
const cors = require('cors')
const app = express();
const port = 8000;
const corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions))
app.use(express.json())
app.use('/products', products);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment