javascript - res.writehead 和 res.write 的 hapi 等价物是什么?

标签 javascript node.js express hapi.js reply

我正在使用 ccavenue npm 模块将支付网关集成到我的网站中。 该模块的问题在于它使用 res.writeres.writehead 而传递给模块的 res 变量是由 hapi 生成的,从而产生错误。那么,res.writehead 和 res.write 的 hapi 等价物是什么呢?

这是我的 hapi 代码:

var ccavenue = require('ccavenue');

ccavenue.setMerchant("******");
ccavenue.setWorkingKey("*******************");
ccavenue.setRedirectUrl("/redirect-url");


module.exports = function(plugin, options, next) {

    plugin.route({
        method: 'GET',
        path: '/checkout',
        handler: function(request, reply) {
            var param = {
                billing_cust_address: 'Bangalore',
                billing_cust_name: 'Nitish Kumar'
            };
        ccavenue.setOrderId("8981455644");
        ccavenue.setOrderAmount("1000");
        ccavenue.setOtherParams(param);
        ccavenue.makePayment(reply);
        }
    });

}

这是模块功能:

function makePayment(res) {
    var errors = helper.checkRequiredField(config);
    if(errors.length > 0) {
        throw new Error(errors);    
    }

    var Checksum = helper.getCheckSum(config.merchantId, config.orderAmount, config.orderId, config.redirectUrl, config.workingKey); //This function is to verify 

    var body = "<form method='post' name='checkout' id='checkout' action='https://www.ccavenue.com/shopzone/cc_details.jsp'>" +
          "<input type=hidden name='Merchant_Id' value='" + config.merchantId + "'>" +
          "<input type=hidden name='Amount' value='" + config.orderAmount + "'>" +
          "<input type=hidden name='Order_Id' value='" + config.orderId + "'>" +
          "<input type=hidden name='Redirect_Url' value='" + config.redirectUrl +"'>" +
          "<input type=hidden name='Checksum' value='" + Checksum + "'>" +
          "<input type=hidden name='TxnType' value='A'>" +
          "<input type=hidden name='ActionID' value='TXN'>";

          for(var key in otherParams) {
                body += "<input type=hidden name='"+ key +"' value='" + otherParams[key] + "'>";
          }

          body += "</form><script type='text/javascript'>" +
              "document.getElementById('checkout').submit();" +
          "</script>";

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

    res.write(body);
    res.end();
}

这是错误:

Debug: internal, implementation, error 
    TypeError: Uncaught error: res.writeHead is not a function
    at Object.makePayment (/home/satnam-sandhu/Workstation/cuboid.io/servers/web/node_modules/ccavenue/index.js:59:8)
    at Object.plugin.route.handler (/home/satnam-sandhu/Workstation/cuboid.io/servers/web/ccavenue/index.js:21:15)
    at Object.exports.execute.internals.prerequisites.internals.handler.finalize [as handler] (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/handler.js:101:51)
    at /home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/handler.js:32:23
    at internals.Protect.run.finish [as run] (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/protect.js:60:12)
    at exports.execute.finalize (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/handler.js:26:22)
    at each (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/request.js:401:16)
    at iterate (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/node_modules/items/lib/index.js:36:13)
    at done (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/node_modules/items/lib/index.js:28:25)
    at internals.Auth.test.internals.Auth._authenticate (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/auth.js:222:16)
    at internals.Auth.test.internals.Auth.authenticate (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/auth.js:197:17)
    at each (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/request.js:401:16)
    at iterate (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/node_modules/items/lib/index.js:36:13)
    at done (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/node_modules/items/lib/index.js:28:25)
    at /home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/lib/protect.js:50:16
    at wrapped (/home/satnam-sandhu/Workstation/cuboid.io/node_modules/hapi/node_modules/hoek/lib/index.js:875:20)

最佳答案

我假设您使用的是旧版本的 hapi 17.0.0,为了使用 html 回复 https://github.com/hapijs/vision 。这个示例教程也应该有所帮助 https://futurestud.io/tutorials/hapi-how-to-render-views 。 Hapi 将为您完成大部分工作。如果您想使用 Node 的 res 和 req http 服务器对象,您需要公开 hapi 内部的原始请求和响应对象,如此处所述 https://hapijs.com/api/16.5.2#request-properties在原始属性(property)下。

关于javascript - res.writehead 和 res.write 的 hapi 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48114051/

相关文章:

node.js - 在端口和将 React/Node 项目部署到 digital ocean 方面遇到问题

mysql - 使用 Node.js 连接到 Google Cloud 上的 MySQL 数据库(错误 : connect ENOENT)

node.js - gulp 中的任务继承,父级 gulpfile.js

node.js - 在使用 Swagger 开发的 NodeJS 上的 ExpressJS 框架中启用跨源资源共享 (CORS)

javascript - expressjs 中简单函数中的未处理 promise 拒绝警告

javascript - NodeJS - 获取二进制/位形式的缓冲区对象

javascript - 尝试制作一个开/关/自动按钮

node.js - 在 docker 容器中使用 multer 和express

node.js - Express.js 单选按钮作为使用渲染参数的表单发布方法

javascript - 将两个 WinJS.Binding.List 合并为一个