簡體   English   中英

如何使Node.js和Glassfish服務器偵聽相同的端口?

[英]How to make Node.js and Glassfish server listen to the same port?

我正在使用運行在端口8080上的Glassfish服務器運行我的Web應用程序。對於同一個Web應用程序,我正在嘗試使用node.js集成Stripe API。 我的Web應用程序的其余部分在localhost:8080上運行。

因此,我該如何通過node.js和glassfish監聽相同的8080端口,以便我的Web應用程序與Stripe node.js集成在一起。

我應該使用網絡套接字嗎?

HTML頁面:

    <body>        

        <form id="form" action="/acctCreated" method="POST">
        <label>Card #: <input type="text" size="16" data-stripe="number" placeholder="Card number" /></label>
        <label>Expiry month: <input type="text" size="2" data-stripe="exp-month" placeholder="MM" /></label>
        <label>year: <input type="text" size="2" data-stripe="exp-year" placeholder="YY" /></label>
        <label>CVC: <input type="text" size="4" data-stripe="cvc" placeholder="CVC" /></label>

        <button type="submit">Pay</button>
    </form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://js.stripe.com/v2/"></script>

    <script type="text/javascript">
        Stripe.setPublishableKey('pk_test_mHCVXXlu5il6pgbQCQzmKY2S');

        var $form = $('#form');
        $form.on('submit', function() {
            // First submit the card information to Stripe to get back a token
            Stripe.card.createToken($form, function(status, response) {
                var token = response.id;

                // Save the token into a hidden input field
                $form.append($('<input type="hidden" name="stripeToken" />').val(token));

                // Now submit the form to our server so it can make the charge against the token
                $form.get(0).submit();
            });
            return false;
        });
    </script>
    </body>

index.js:

    var express = require('express');
    var bodyParser = require('body-parser');
    var stripe = require('stripe')('sk_test_bpfjQsY5iK7ZI7W5tJMKpPli');
    var http = require('http');

    var app = express();
    app.use(bodyParser.urlencoded({
          extended: true
    }));
    app.use(bodyParser.json());

      app.post('/acctCreated', function(req, res) { 
        console.log('Inside charge');
        //var stripeToken = req.body.stripeToken;
        var stripeToken = req.body.id;
        var amount = 1000;

        console.log('Calculating charge');
        stripe.charges.create({
            card: stripeToken,
            currency: 'usd', 
            amount: amount
        },
        function(err, charge) {
            if (err) {
                res.send(500, err);
                //res.status(500).send(err);
            } else {
                res.send(204);
                //res.status(204).send(charge);
            }
        });

          var path = "http://localhost:8080/TropoHotelReserv/faces/roomsBooked.xhtml"  ;
          console.log("Get PathName " + path);
          res.writeHead(302, {'Location': path});
          res.end();

        console.log('Complete');
    });

    app.use(express.static(__dirname));
    app.listen(process.env.PORT || 8080);

謝謝,

您不能直接這樣做。 您需要在8080前面監聽的負載均衡器/路由器。

在端口8081上運行Glassfish,在8082上運行Node.js,然后在前面使用負載均衡器(例如stud,haproxy,apache httpd或varnish),並將localhost:8081和localhost:8082設置為相應URL路徑的后端。

這是使用HAProxy的示例

您可以使用單個HAProxy服務器基於URL和負載平衡分離請求。 您的配置將具有以下內容:

 frontend http acl app1 path_end -i /app1/123 #matches path ending with "/app/123" acl app2 path_end -i /app2/123 acl app3 path_end -i /app3/123 use_backend srvs_app1 if app1 use_backend srvs_app2 if app2 use_backend srvs_app3 if app3 backend srvs_app1 #backend that lists your servers. Use a balancing algorithm as per your need. balance roundrobin server host1 REGION1_HOST_FOR_APP1:PORT server host2 REGION2_HOST_FOR_APP1:PORT backend srvs_app2 balance roundrobin server host1 REGION1_HOST_FOR_APP2:PORT server host2 REGION2_HOST_FOR_APP2:PORT backend srvs_app3 balance roundrobin server host1 REGION1_HOST_FOR_APP3:PORT server host2 REGION2_HOST_FOR_APP3:PORT 

可以在[主頁] [1]上找到更多信息。

[1]: http//haproxy.1wt.eu/download/1.5/doc/configuration.txt

資料來源: https : //stackoverflow.com/a/20640578

在Windows上,您可以在Apache httpd中使用mod_proxy:

ProxyPass /nodejspath http://localhost:8081/nodejspath
ProxyPass /glassfishpath http://localhost:8081/glassfishpath

更多信息: http : //httpd.apache.org/docs/2.2/mod/mod_proxy.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM