我正在使用在端口 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 的示例
You can segregate requests based on URL and load balance with a single HAProxy server. 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]: 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
关于node.js - 如何让 Node.js 和 Glassfish 服务器监听同一个端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590141/