res.end(result) 没有在我的网页上显示结果
res.end(result) doesn't display the result on my web page
调用 res.end(result)
不会在我的网页上显示结果,当我调用 console.log('result ' + result)
.
时它只会显示在控制台上
这是我的 nodejs 代码:
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 !')
})
我一直在函数内部和外部尝试"res.end(result)" 'getData'但是同样的问题,网页上没有任何显示
你能试试下面的方法吗:
res.write(result);
res.end()
res 是一个 stream
所以你需要把你的结果写在 res
然后 end
它
祝你有愉快的一天!
好吧,你只在 web3.isConnected()
和 q.opt == 'get' && typeof q.index != "undefined"
时才发回回复。可能是其中一个条件不成立。
这是一个艰难的坚果,但我想我做到了。
有关如何在 Express 中设置 headers 以及如何在 Express 中发送数据,请参阅 here and here and here。您将 Node 的 res
/req
与 Express 的 res
/req
混合在一起。不要那样做。 Express 包装 Node-objects 并且工作方式略有不同,例如它会为您处理正确的 headers。
这就是您正在做的:使用 writeHead()
,您设置了 headers。然后,您使用 .end()
和错误的 argument-type: res.end()
will only accept string
s or Buffer
s 作为数据(第一个错误)。当您更改为 .send()
时,该方法负责设置正确的 headers,但您已经手动完成了该操作,因此您设置了两次 headers,您不应该这样做 boom(第二个错误)。
解决办法是删除这个:
res.writeHead(200, {
'Content-Type': 'text/html'
});
...然后使用 res.send(result)
发送您的数据。
作为旁注,我 喜欢 了解您 运行 您的应用程序的情况并且没有收到错误日志,因为当我试图建立一个可验证的例子。详细的错误日志有时比创建它们的代码更有价值。这是其中之一 "sometimes".
调用 res.end(result)
不会在我的网页上显示结果,当我调用 console.log('result ' + result)
.
这是我的 nodejs 代码:
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 !')
})
我一直在函数内部和外部尝试"res.end(result)" 'getData'但是同样的问题,网页上没有任何显示
你能试试下面的方法吗:
res.write(result);
res.end()
res 是一个 stream
所以你需要把你的结果写在 res
然后 end
它
祝你有愉快的一天!
好吧,你只在 web3.isConnected()
和 q.opt == 'get' && typeof q.index != "undefined"
时才发回回复。可能是其中一个条件不成立。
这是一个艰难的坚果,但我想我做到了。
有关如何在 Express 中设置 headers 以及如何在 Express 中发送数据,请参阅 here and here and here。您将 Node 的 res
/req
与 Express 的 res
/req
混合在一起。不要那样做。 Express 包装 Node-objects 并且工作方式略有不同,例如它会为您处理正确的 headers。
这就是您正在做的:使用 writeHead()
,您设置了 headers。然后,您使用 .end()
和错误的 argument-type: res.end()
will only accept string
s or Buffer
s 作为数据(第一个错误)。当您更改为 .send()
时,该方法负责设置正确的 headers,但您已经手动完成了该操作,因此您设置了两次 headers,您不应该这样做 boom(第二个错误)。
解决办法是删除这个:
res.writeHead(200, {
'Content-Type': 'text/html'
});
...然后使用 res.send(result)
发送您的数据。
作为旁注,我 喜欢 了解您 运行 您的应用程序的情况并且没有收到错误日志,因为当我试图建立一个可验证的例子。详细的错误日志有时比创建它们的代码更有价值。这是其中之一 "sometimes".