-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
49 lines (40 loc) · 1.35 KB
/
index.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
// Import necessary modules
const express = require('express');
const path = require('path');
const Web3 = require('web3');
const contract = require('@truffle/contract');
// Initialize Express app
const app = express();
// Set up middleware
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// Initialize Web3 and connect to the Ethereum network
const web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
const web3 = new Web3(web3Provider);
// Load your smart contract ABI and address
const contractAbi = require('./build/contracts/YourContract.json').abi;
const contractAddress = '0xYourContractAddress'; // Replace with your contract address
// Create a contract object
const yourContract = contract({
abi: contractAbi
});
yourContract.setProvider(web3Provider);
const contractInstance = yourContract.at(contractAddress);
// Define routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/getData', async (req, res) => {
try {
const data = await contractInstance.getData();
res.json({ data });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});