forked from BrianWangila/NodeJS-CRUD-PostgresSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 1.68 KB
/
index.js
File metadata and controls
68 lines (59 loc) · 1.68 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
require('dotenv').config();
const { response } = require('express');
//const { response } = require('express');
const express = require('express');
//const { Pool } = require('pg');
const app = express();
//const cors = require('cors');
const port = process.env.PORT_SRV || 3001;
const merchant_model = require('./merchant_model');
app.use(express.json());
//app.use(cors());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers');
next();
});
app.get('/', (req, res) => {
merchant_model.getMerchants()
.then(response => {
res.status(200).send(response);
//console.log(res)
})
.catch(error => {
res.status(500).send(error);
})
});
app.post('/merchants', (req, res) => {
merchant_model.createMerchant(req.body)
.then(response => {
res.status(200).send(response)
})
.catch(error => {
res.status(500).send(error)
})
});
app.put('/merchants/:id', (req, res) => {
const id = req.params.id
const body = req.body
merchant_model.updateMerchant(id, body)
.then(response => {
res.status(200).send(response)
})
.catch(error => {
res.status(500).send(error)
})
});
app.delete('/merchants/:id', (req, res) => {
merchant_model.deleteMerchant(req.params.id)
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
app.listen(port, () => {
console.log(`App running on port ${port}.`);
});