如何让Node.js和Glassfish服务器监听同一个端口?

我正在使用运行在端口8080上的Glassfish服务器运行我的Web应用程序。对于同一个Web应用程序,我正在尝试使用node.js集成Stripe API。 其余的web应用程序在localhost:8080上运行。

那么如何通过node.js和glassfish监听同一个8080端口,以便我的Web应用程序与Stripe node.js集成。

我应该使用networking套接字?

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设置为相应URLpath的后端。

这是一个使用HAProxy的例子

您可以使用单个HAProxy服务器根据URL和负载平衡分隔请求。 你的configuration将有这样的东西:

 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]上find。

[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

Interesting Posts