如何让 Node.js 和 Glassfish 服务器监听同一个端口?
How to make Node.js and Glassfish server listen to the same port?
我 运行 我的 Web 应用程序使用端口 8080 上 运行 的 Glassfish 服务器。对于同一个 Web 应用程序,我正在尝试使用 [=26] 集成 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 的负载 balancer/router。
运行 端口 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
来源:
在 Windows 上,您可以在 Apache httpd:mod_proxy 中使用 mod_proxy:
ProxyPass /nodejspath http://localhost:8081/nodejspath
ProxyPass /glassfishpath http://localhost:8081/glassfishpath
我 运行 我的 Web 应用程序使用端口 8080 上 运行 的 Glassfish 服务器。对于同一个 Web 应用程序,我正在尝试使用 [=26] 集成 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 的负载 balancer/router。
运行 端口 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
来源:
在 Windows 上,您可以在 Apache httpd:mod_proxy 中使用 mod_proxy:
ProxyPass /nodejspath http://localhost:8081/nodejspath
ProxyPass /glassfishpath http://localhost:8081/glassfishpath