Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Express = require('Express')
- var app = new Express()
- var {Client} = require('pg')
- const client = new Client({user: 'hgpdgbosvgatfe',
- host: 'ec2-50-16-196-138.compute-1.amazonaws.com',
- database: 'durmvjt2ug0c1',
- password: '7d88c3deb02ef15d683435b3906aedc7c9b8b6a0db0a3109943d37e8dcccdb54',
- port: 5432,
- ssl: true});
- client.connect().then(res => {res}, err =>{console.log(err)});
- app.get('/addProduct', (req, res) =>
- {
- if (!req.query.name || !req.query.description || !req.query.price)
- {
- res.send("Заполните все поля (name, description, price)")
- }
- else
- {
- client.query("INSERT INTO products (name, description, price) VALUES"+
- `(\'${req.query.name}\', \'${req.query.description}\', ${req.query.price} )`, (err, result) =>
- {
- if (err)
- {
- res.send(err)
- }
- else
- {
- res.send("Продукт успешно добавлен")
- }
- })
- }
- })
- app.get('/getProduct', (req,res) =>
- {
- if(req.query.id)
- {
- client.query(`SELECT * FROM products WHERE id =${req.query.id}`, (err,result) =>
- {
- if (err)
- {
- res.send(err)
- }
- else
- {
- res.send(result.rows)
- }
- })
- }
- else if(req.query.name)
- {
- client.query(`SELECT * FROM products WHERE name =\'${req.query.name}\'`, (err,result) =>
- {
- if (err)
- {
- res.send(err)
- }
- else
- {
- res.send(result.rows)
- }
- })
- }
- else
- {
- res.send("заполните имя или ид продукта ")
- }
- })
- app.get('/order', (req, res) =>
- {
- if (!req.query.phone || !req.query.adress || !req.query.product || !req.query.price || !req.query.count)
- {
- res.send("Заполните все поля заказа")
- }
- else
- {
- client.query("INSERT INTO orders (phone, adress, product, price, count) Values"+
- `(\'${req.query.phone}\', \'${req.query.adress}\', ${req.query.product}, ${req.query.price}, ${req.query.count})`,(err, result) =>
- {
- if (err)
- {
- res.send(err)
- }
- else
- {
- res.send("Заказ успешно добавлен")
- }
- })
- }
- })
- app.get('/getOrders', (req,res) =>
- {
- if(req.query.phone)
- {
- client.query(`Select * from orders where phone =\'${req.query.phone}\'`,(err, result)=>
- {
- if (err)
- {
- res.send(err);
- }
- else
- {
- res.send(result.rows)
- }
- })
- }
- else
- {
- res.send("Введите номер телефона")
- }
- })
- app.listen(8080)
Add Comment
Please, Sign In to add comment