-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
49 lines (37 loc) · 1.21 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
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
const port = process.env.PORT || 5000;
axios.defaults.headers.post['Content-Type'] = 'application/json';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/api/hello', (req, res) => {
res.send({ express: 'Hello From Express' });
});
app.get('/blockchain/blocks',(req, res) => {
axios.get('http://localhost:4001/blocks')
.then(response => {
res.send(response.data)
})
.catch(error => {console.log('error: '+error)})
});
// Blockchain call for adding new block
app.post('/blockchain/mineBlock',(req,res)=>{
// port 4001 runs the http api for the blockchain component.
axios.post('http://localhost:4001/mineBlock',{
data: req.body.data
}).then(response => {
console.log(response.data);
}).catch(error => {
console.log('error: '+ error);
});
res.json({msg: "Block Added!"})
});
app.post('/api/world', (req, res) => {
console.log(req.body);
res.send(
`I received your POST request. This is what you sent me: ${req.body.post}`,
);
});
app.listen(port, () => console.log(`Listening on port ${port}`));