我正在使用在端口8080上运行的Glassfish服务器运行我的Web应用程序。对于同一个Web应用程序,我尝试使用node.js集成Stripe API。我的其他Web应用程序在localhost:8080上运行。
那么我如何通过node.js和glassfish监听相同的8080端口,以便我的Web应用程序与Stripe node.js集成。
我应该使用网络套接字吗?
HTML页面:
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);
index.js:
int pid = fork();
if(pid == 0)
{
close(tcp_socket);
char buffer[255]={0};
int bytes_num;
char serID[] = "0";
//std::string serID = "0";
// char serID[7] = "0";
while(true)
{
//recv(int sockfd, void *buf, size_t len, int flags);
bytes_num = recv(tcp_socket2,buffer,254,0);
if(bytes_num == 0)
{
break;
}
if(serID[] == "0")---------------->problem here
{
serID[]= buffer[0];
}
printf("serverID %s\n", serID);
if(serID[]=="serverA")---------------->problem here
{
//printf("NOW files!\n");
strcpy(str_file1[msg_num1],buffer);
msg_num1++;
}
if(serID[]=="serverB")---------------->problem here
{
strcpy(str_file2[msg_num2],buffer);
msg_num2++;
}
if(serID[]=="serverC")---------------->problem here
{
strcpy(str_file3[msg_num3],buffer);
msg_num3++;
}
if(serID[]=="serverD")---------------->problem here
{
strcpy(str_file4[msg_num4],buffer);
msg_num4++;
}
memset(buffer,0,sizeof(buffer));
}
if((serID[]=="serverA"))---------------->problem here
{
pf_file1 = fopen("rcvd_fileA.txt","w");
for(int i=0; i< msg_num1;i++)
{
//printf("now we read files!\n");
fprintf(pf_file1,"%s\n",str_file1[i]);
}
fclose(pf_file1);
}
if((serID[]=="serverB"))---------------->problem here
{
pf_file2 = fopen("rcvd_fileB.txt","w");
for(int i=0; i< msg_num2;i++)
{
fprintf(pf_file2,"%s\n",str_file2[i]);
}
fclose(pf_file2);
}
if((serID[]=="serverC"))---------------->problem here
{
pf_file3 = fopen("rcvd_fileC.txt","w");
for(int i=0; i< msg_num3;i++)
{
fprintf(pf_file3,"%s\n",str_file3[i]);
}
fclose(pf_file3);
}
if((serID[]=="serverD"))---------------->problem here
{
pf_file4 = fopen("rcvd_fileD.txt","w");
for(int i=0; i< msg_num4;i++)
{
fprintf(pf_file4,"%s\n",str_file4[i]);
}
fclose(pf_file4);
}
printf("The Client receives neighbor information from the Server %s with TCP port number %d and IP address %s \n",serID.c_str(),ntohs(sin.sin_port), ipstr);
close(tcp_socket2);
// printf("The Server %c has the following neighbor information: \n",serverID);
return 0;
}
谢谢,
答案 0 :(得分:1)
你不能直接这样做。你需要一个负载平衡器/路由器在前面听8080。
在端口8081上运行Glassfish,在8082上运行Node.js,然后在前面使用负载均衡器(例如stud,haproxy,apache httpd或varnish)并设置localhost:8081和localhost:8082作为相应的后端网址路径。
以下是以这种方式使用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_proxyProxyPass /nodejspath http://localhost:8081/nodejspath
ProxyPass /glassfishpath http://localhost:8081/glassfishpath