-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (82 loc) · 2.72 KB
/
app.js
File metadata and controls
96 lines (82 loc) · 2.72 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
const { sequelize } = require('./models');
const express = require('express');
const bcrypt = require('bcryptjs');
const app = express();
const port = 3000;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.post('/users', async (req, res) => {
try{
const {name, email, password} = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const user = await sequelize.models.user.create({name, email, password: hashedPassword});
res.json(user);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.post('/expenses', async (req, res) => {
try{
const {salary, description} = req.body;
const expenses = await sequelize.models.expenses.create({salary, description});
res.json(expenses);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.get('/expenses', async (req, res) => {
try{
const expenses = await sequelize.models.expenses.findAll();
res.json(expenses);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.get('/users', async (req, res) => {
try{
const users = await sequelize.models.user.findAll();
res.json(users);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.delete('/expenses/:id', async (req, res) => {
try{
const {id} = req.params; const expenses = await sequelize.models.expenses.destroy({where: {id}});
res.json(expenses);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.patch('/expenses/:id', async (req, res) => {
try{
const {id} = req.params;
const {salary, description} = req.body;
const expenses = await sequelize.models.expenses.update({salary, description}, {where: {id}});
res.json(expenses);
}
catch(error){
console.error(error);
res.status(500).json({error: 'Something went wrong,please try again'});
}
});
app.listen(port, () => {
console.log(`App is running at http://localhost:${port}`);
sequelize.authenticate().then(() => {
console.log('Database connected');
}).catch((error) => {
console.log('Error connecting to database,please confirm your credentials', error);
})
});