Calling res.end(result)
doesn't display the result on my web page, it is just displayed on the console when I call console.log('result ' + result)
.
this is my nodejs code:
const express = require('express')
const app = express()
var url = require('url');
var Web3 = require('web3');
app.get('/', function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("http://54.00.00.00:3010"));
}
if (web3.isConnected()) {
var abi = JSON.parse('[{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getData","outputs":[{"name":"","type":"bytes32"},{"name":"","type":"uint256"},{"name":"","type":"bytes32"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"Achats","outputs":[{"name":"aName","type":"bytes32"},{"name":"aId","type":"uint256"},{"name":"aDate","type":"bytes32"},{"name":"aValue","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"name","type":"bytes32"},{"name":"iid","type":"uint256"},{"name":"date","type":"bytes32"},{"name":"value","type":"uint256"}],"name":"setData","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]');
TestContract = web3.eth.contract(abi);
contractInstance = TestContract.at('0xc08f46fff9fcf082a0d1cebbcc464ca141d2b7d7');
if (q.opt == 'get' && typeof q.index != "undefined") {
contractInstance.getData.call(parseInt(q.index), {
from: '0x3e10889Ef5066C1428ECaf8580aD4EFd50F8Cf7B'
}, function(error, result) {
console.log('error ' + error);
console.log('result ' + result);
res.end(result);
});
}
}
}
app.listen(3000, function() {
console.log('Yep... on 3000 !')
})
i have been trying "res.end(result)" inside and outside of the function 'getData' but the same problem, nothing displayed on the web page
This was a tough nut, but I think I got it.
See here and here and here for how to set headers in Express and how to send data in Express. You are mixing Node's res
/req
with Express' res
/req
. Don't do that. Express wraps the Node-objects and works slightly different, e.g. it takes care about the correct headers for you.
This is what you are doing: Using writeHead()
, you set the headers. Then, you use .end()
with the wrong argument-type: res.end()
will only accept string
s or Buffer
s as data (first error). When you change to .send()
, that method takes care of setting the correct headers, but you had already done that manually, so you set the headers twice and you should not do that so boom (second error).
Solution is to remove this:
res.writeHead(200, {
'Content-Type': 'text/html'
});
... and then use res.send(result)
to send your data.
As a side note, I would love to know how you are running your app and not getting error logs, because they were all over the place when I tried to build a verifiable example. Verbose error logs are sometimes worth more than the code that created them. This is one of those "sometimes".