-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.34 KB
/
Copy pathindex.js
File metadata and controls
55 lines (45 loc) · 1.34 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
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const Web3 = require('web3');
const {Contract,ethers}=require('ethers');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Parse JSON bodies for this app
app.use(bodyParser.json());
let receivedData;
// Choose a port number for your API
app.use(cors());
app.post('/', async (req, res) => {
const data = req.body; // Get the JSON data from the request body
try {
// Process the data here
console.log(data);
receivedData=data;
// Send a response back to the client
res.json({ message: 'Data received successfully', data: data });
} catch (error) {
console.log(error);
res.status(500).send('Error processing data');
}
});
// Define a route for retrieving data with correct headers
app.get('/', async (req, res) => {
// Get the CID from the query string
try {
const response = await axios.get(receivedData.uri, {
headers: {
'Accept': 'text/plain'
}
});
res.send(response.data); // Send the response data back to the client
} catch (error) {
console.log(error);
res.status(500).send('Error retrieving data from Pinata');
}
});
// Start the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});