forked from Raghu150999/BESit-DBS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (70 loc) · 2.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const routers = require('./server/routes');
const cors = require('cors');
const mysql = require('mysql');
const app = express();
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'besit',
insecureAuth: true
});
connection.connect((err) => {
if (err) {
throw err;
}
else {
console.log('Connected to DB');
}
});
// environment variables for jwt token
require('dotenv').config();
// Middleware Setup
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', routers);
// Custom middleware to handle errors
app.use((err, req, res, next) => {
console.log('Error:', err.message);
res.status(422).json(err.message);
});
const port = process.env.NODE_ENV ? process.env.PORT : 8000;
// Route for image upload(s)
app.post('/uploaditem', (req, res) => {
let query;
query = "select max(_id) as lst from product";
connection.query(query, (err, result) => {
if (err) throw err;
let _id = result[0].lst + 1;
let name = req.body.name;
let price = req.body.price;
let status = req.body.status;
let description = req.body.desc;
let timestamp = new Date().toISOString().slice(0, 19).replace('T', ' ');
let owner = req.body.owner;
let category_name = req.body.category;
query = "insert into product values (" + _id + ", '" + name + "', " + price + ", '" + status + "', '" + description + "', '" + timestamp + "', '" + owner + "', '" + category_name + "');";
connection.query(query, (err, result) => {
if (err) throw err;
res.send('ok');
});
});
});
// @route: for removing item from database
app.post('/removeitem', (req, res) => {
const item = req.body;
let query;
query = "delete from product where _id = " + item._id + ";";
connection.query(query, (err, result) => {
if (err) throw err;
res.send('ok');
});
});
app.listen(process.env.PORT || 8000);
console.log(`Listening to port ${port}`);
module.exports.connection = connection;