-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (43 loc) · 1.26 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
const express = require('express')
const db = require('./db')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json())
const port = 8080
app.get('/users', async (req, res) => {
const { userName, password } = req.query;
try {
const query = 'SELECT * FROM users WHERE username = $1 AND password = $2';
const values = [userName, password];
const result = await db.query(query, values);
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.post('/users', async (req, res) => {
const { firstName, lastName, userName, password } = req.body;
try {
const query = `
INSERT INTO users (first_name, last_name, username, password)
VALUES ($1, $2, $3, $4)
`;
const values = [firstName, lastName, userName, password];
await db.query(query, values);
res.status(201).json({ message: 'User created successfully' });
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Server error' });
}
})
app.get('/items', (req, res) => {
res.send('Here are your items!')
})
app.post('/items', (req, res) => {
res.send('You have created a new Item')
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})