简体   繁体   English

如何使Node.js和Glassfish服务器侦听相同的端口?

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

I am running my web application using Glassfish server which is running on port 8080. For the same web application, I am trying to integrate Stripe API using node.js. 我正在使用运行在端口8080上的Glassfish服务器运行我的Web应用程序。对于同一个Web应用程序,我正在尝试使用node.js集成Stripe API。 Rest of my web application runs on localhost:8080. 我的Web应用程序的其余部分在localhost:8080上运行。

So how do I listen to the same 8080 port through node.js and also glassfish for so that my web application is integrated with Stripe node.js. 因此,我该如何通过node.js和glassfish监听相同的8080端口,以便我的Web应用程序与Stripe node.js集成在一起。

Should I use web sockets? 我应该使用网络套接字吗?

HTML page: 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: 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);

Thanks, 谢谢,

You can't do that directly. 您不能直接这样做。 You need a load balancer/router in front listening on 8080. 您需要在8080前面监听的负载均衡器/路由器。

Run Glassfish on port 8081 and Node.js on 8082, then use a load balancer (ex. stud, haproxy, apache httpd, or varnish) in front and set up localhost:8081 and localhost:8082 as backends for the corresponding URL paths. 在端口8081上运行Glassfish,在8082上运行Node.js,然后在前面使用负载均衡器(例如stud,haproxy,apache httpd或varnish),并将localhost:8081和localhost:8082设置为相应URL路径的后端。

Here's an example of using HAProxy that way 这是使用HAProxy的示例

You can segregate requests based on URL and load balance with a single HAProxy server. 您可以使用单个HAProxy服务器基于URL和负载平衡分离请求。 Your configuration will have something like this: 您的配置将具有以下内容:

 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 

More information can be found on the [homepage][1]. 可以在[主页] [1]上找到更多信息。

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

Source: https://stackoverflow.com/a/20640578 资料来源: https : //stackoverflow.com/a/20640578

On Windows you could use mod_proxy in Apache httpd: 在Windows上,您可以在Apache httpd中使用mod_proxy:

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

More info: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html 更多信息: 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