代码之家  ›  专栏  ›  技术社区  ›  Mounir Atouil

res.end(result)不在我的网页上显示结果

  •  0
  • Mounir Atouil  · 技术社区  · 7 年前

    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 !')
            })
    

    我一直在尝试函数“getData”内外的“res.end(result)”,但同样的问题是,网页上没有显示任何内容

    3 回复  |  直到 7 年前
        1
  •  1
  •   Oliver Baumann    7 年前

    这是一个 艰苦的 坚果,但我想我明白了。

    看见 here here here 了解如何在Express中设置标头以及如何在Express中发送数据。您正在混合节点的 res / req 物件 / 要求 . 不要那样做。Express包装节点对象,工作方式稍有不同,例如,它为您考虑正确的标头。

    这就是你正在做的:使用 writeHead() ,您可以设置标题。然后,您使用 .end() 参数类型错误: res.end() will only accept string s or Buffer s .send() ,该方法负责设置正确的标头,但您已经手动完成了,因此您设置了两次标头,不应该这样做 繁荣 (第二个错误)。

    res.writeHead(200, {
        'Content-Type': 'text/html'
    });
    

    ... 然后使用 res.send(result) 发送数据。

    作为补充,我会 我想知道你是如何运行你的应用程序而没有得到错误日志的,因为当我试图构建一个可验证的示例时,错误日志到处都是。详细的错误日志有时比创建它们的代码更有价值。这是一种“有时”。

        2
  •  0
  •   Adrien De Peretti    7 年前

    你能试试下面的东西吗

        res.write(result);
        res.end()
    

    res是一个 stream 所以你需要把结果写在 res 然后 end

    祝您有个美好的一天!

        3
  •  0
  •   Sello Mkantjwa    7 年前

    嗯,只有在以下情况下,你才会回复 web3.isConnected() q.opt == 'get' && typeof q.index != "undefined" . 可能是其中一个条件不成立。