简体   繁体   English

res.writehead和res.write的hapi等效项是什么?

[英]What is the hapi equivalent of res.writehead and res.write?

I am using ccavenue npm module to integrate payment gateway in my website. 我正在使用ccavenue npm模块将支付网关集成到我的网站中。 The problem with that modules is that it uses res.write and res.writehead while the res variable passed to the module is generated by hapi and thus generating a error. 该模块的问题在于,它使用res.writeres.writehead而传递给模块的res变量是由hapi生成的,因此会产生错误。 So, what is the hapi equivalent of res.writehead and res.write . 因此, res.writeheadres.write的hapi等效项是res.write

Here is my hapi code: 这是我的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);
        }
    });

}

This is the module function: 这是模块功能:

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();
}

This is the error: 这是错误:

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)

I assume you are using an older version of hapi 17.0.0, in order to to reply with html use https://github.com/hapijs/vision . 我假设您使用的是hapi 17.0.0的旧版本,以便使用HTML进行回复,请使用https://github.com/hapijs/vision Also this example tutorial should help https://futurestud.io/tutorials/hapi-how-to-render-views . 此外,此示例教程还应帮助https://futurestud.io/tutorials/hapi-how-to-render-views Hapi will do most of the work for you. Hapi将为您完成大部分工作。 If you want to use node's res and req http server objects you need to expose the raw request and response objects from hapi's internals as documented here https://hapijs.com/api/16.5.2#request-properties under raw property. 如果要使用节点的res和req http服务器对象,则需要暴露来自hapi内部的原始请求和响应对象,如https://hapijs.com/api/16.5.2#request-properties在raw属性下所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM