Commit 9996b974 authored by Vadim's avatar Vadim

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

parent 4481e41e
......@@ -12,6 +12,9 @@ module.exports = {
console.error(e);
data = []
}
},
getItem(id){
},
getItems() {
return data;
......
const MongoClient = require('mongodb').MongoClient;
let db = null;
let client = null;
const connect = async () => {
client = await MongoClient.connect("mongodb://localhost");
db = client.db('shop')
};
const disconnect = async () => {
await client.disconnect();
};
module.exports = {
connect,
disconnect,
getDb: () => db
};
\ No newline at end of file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProductSchema = new Schema({
title: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
description: String,
image: String
}, {
versionKey: false
});
const Product = mongoose.model('Product', ProductSchema);
module.exports = Product;
......@@ -4,6 +4,7 @@ const path = require('path');
const router = express.Router();
const {nanoid} = require('nanoid');
const config = require('./config');
const Product = require('./models/Products');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
......@@ -15,25 +16,39 @@ const config = require('./config');
});
const upload = multer({storage});
const createRouter = (db) => {
const createRouter = () => {
router.get('/', async (req, res) => {
try {
const products = await Product.find();
res.send(products);
} catch (e) {
res.status(500).send(e);
}
const products = await db.getItems('products');
res.send(products);
});
router.get('/:id', async (req, res) => {
const product = await db.getItem('products', req.params.id);
res.send(product[0]);
try {
const result = await Product.findById(req.params.id);
res.send(result);
}catch (e) {
res.sendStatus(404);
}
});
router.post('/', upload.single('image'), async (req, res) => {
const product = req.body;
const product = new Product(req.body);
if(req.file) {
product.image = req.file.filename;
}
const newProduct = await db.addItem('products', product);
res.send(newProduct);
try {
await product.save();
res.send(product);
} catch (e) {
res.status(500).send(e);
}
});
return router;
......
This diff is collapsed.
......@@ -12,6 +12,8 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongodb": "^3.6.3",
"mongoose": "^5.11.8",
"multer": "^1.4.2",
"mysql": "^2.18.1",
"nanoid": "^3.1.18"
......
const express = require('express');
const products = require('./app/products');
const cors = require('cors');
const mysql = require('mysql');
const config = require('./app/config');
const db = require('./app/db/mysql');
const mongoose = require('mongoose');
const app = express();
const port = 8000;
const connection = mysql.createConnection(config.db);
const corsOptions = {
origin: 'http://localhost:3000',
......@@ -20,16 +17,16 @@ app.use(express.json());
app.use(express.static('public'));
connection.connect(err => {
if(err){
console.error(err);
throw err;
}
app.use('/products', products(db(connection)));
const run = async () => {
await mongoose.connect("mongodb://localhost/shop", {useNewUrlParser: true});
app.use('/products', products());
app.listen(port, () => {
console.log(`Server started on port ${port}`)
});
console.log('mysql connected!');
});
console.log('mongo connected!');
};
run().catch(console.log);
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