-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (115 loc) · 4.41 KB
/
server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { createServer } = require('http');
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const axios = require('axios');
var bodyParser = require('body-parser');
const normalizePort = port => parseInt(port, 10);
const PORT = normalizePort(process.env.PORT || 5000);
const app = express()
const dev = app.get('env') !== 'production'
if (!dev){
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.disable('x-powered-by')
app.use(compression())
app.use(morgan('common'))
app.use(express.static(path.resolve(__dirname, 'build')))
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', (req, res, next) => {
console.log('Main page is called')
res.sendFile(path.resolve(__dirname, 'build', 'index.html'))
})
// app.get('/students', (req, res) => {
// console.log('students is called');
// res.send('hello world');
// });
app.post('/creates', (req, res, next) => {
console.log('server students is created');
//res.send('hello world creates');
//NODE_ENV=production node app.js
// console.log('req')
// console.log(req)
console.log('req.body')
console.log(req.body)
axios.post('http://springcrudapp-springcrudapp.1d35.starter-us-east-1.openshiftapps.com/SpringRestHibernate/create', req.body)
.then((response) => {
console.log('Server response -> this is being sent stringified')
console.log("response.data");
console.log(response.data);
res.send(JSON.stringify(response.data))
})
.catch((error) => {
console.log(error);
});
});
app.post('/updates', (req, res, next) => {
console.log('server update students is created');
//res.send('hello world creates');
//NODE_ENV=production node app.js
// console.log('req')
// console.log(req)
console.log('req.body')
console.log(req.body)
axios.put('http://springcrudapp-springcrudapp.1d35.starter-us-east-1.openshiftapps.com/SpringRestHibernate/update', req.body)
.then((response) => {
console.log('Server response -> this is being sent stringified for update')
console.log("response.data");
console.log(response.data);
res.send(JSON.stringify(response.data))
})
.catch((error) => {
console.log(error);
});
});
app.post('/deletes', (req, res, next) => {
console.log('server update students is deleted');
//res.send('hello world creates');
//NODE_ENV=production node app.js
// console.log('req')
// console.log(req)
console.log('req.body')
console.log(req.body)
axios.delete('http://springcrudapp-springcrudapp.1d35.starter-us-east-1.openshiftapps.com/SpringRestHibernate/delete/' + req.body.id)
.then((response) => {
console.log('Server response -> this is being sent stringified for delete')
console.log("response.data");
console.log(response.data);
res.send(JSON.stringify(response.data))
})
.catch((error) => {
console.log(error);
});
});
app.get('/students', (req, res, next) => {
console.log('students is called');
//res.send('hello world');
axios.get('http://springcrudapp-springcrudapp.1d35.starter-us-east-1.openshiftapps.com/SpringRestHibernate/students')
.then( (response) => {
console.log("Server Response")
console.log("Response")
console.log(response)
console.log("Response.data")
console.log(response.data)
let stringify = JSON.stringify(response.data)
res.send(stringify);
})
.catch( (error) => {
console.log(error);
});
});
}
if (dev){
app.use(morgan('dev'))
console.log(" Is this dev")
}
const server = createServer(app)
server.listen(PORT, err => {
if (err) throw err
console.log('Server Started');
})