nodejs无法响应连接超时

时间:2017-03-07 11:39:59

标签: javascript node.js websocket

我在我的server.js中有这个:

var app = require('express')();
var http = require('http');
http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.write("It's alive!");
      response.end();
}).listen(3000);

这在本地工作正常但不是在我部署它并尝试访问我的shared.domain.ma:3000时 在客户端,我有一个Websocket试图连接到我的服务器在该地址,但它给了我net::ERR_CONNECTION_TIMED_OUT安,我尝试了curl,结果再次相同 - 这个问题可能是什么原因?

2 个答案:

答案 0 :(得分:0)

您提供的信息非常少,但在您的一些评论之后更清楚:

  是的,我刚检查了该死的防火墙..我不能使用80或8080因为它们已经被使用了!如何为这个子域转发3000到80

我认为你想要转发80到3000,而不是相反。

您没有说明您使用什么服务器进行反向代理。如果它是nginx,那么你可以使用这样的东西:

    static void Main(string[] args)
    {
        UserCredential credential;

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart2.json");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Gmail API service.
        var service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });


        var re = service.Users.Messages.List("me");
        re.LabelIds = "INBOX";
        re.Q = "is:unread"; //only get unread;

        var res = re.Execute();

        if (res != null && res.Messages != null)
        {
            Console.WriteLine("there are {0} emails. press any key to continue!", res.Messages.Count);
            Console.ReadKey();

            foreach (var email in res.Messages)
            {
                var emailInfoReq = service.Users.Messages.Get("me", email.Id);
                var emailInfoResponse = emailInfoReq.Execute();

                if (emailInfoResponse != null)
                {
                    String from = "";
                    String date = "";
                    String subject = "";
                    String body = "";
                    //loop through the headers and get the fields we need...
                    foreach (var mParts in emailInfoResponse.Payload.Headers)
                    {
                        if (mParts.Name == "Date")
                        {
                            date = mParts.Value;
                        }
                        else if (mParts.Name == "From")
                        {
                            from = mParts.Value;
                        }
                        else if (mParts.Name == "Subject")
                        {
                            subject = mParts.Value;
                        }

                        if (date != "" && from != "")
                        {
                            if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
                                body = DecodeBase64String(emailInfoResponse.Payload.Body.Data);
                            else
                                body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");

                            //now you have the data you want....

                        }

                    }

                    //Console.Write(body);
                    Console.WriteLine("{0}  --  {1}  -- {2}", subject, date, email.Id);
                    Console.ReadKey();
                }
            }
        }
    }

有关详细信息,请参阅此答案:

当然你可以用Apache做同样的事情,只是使用不同的配置。搜索反向代理配置。

答案 1 :(得分:0)

如评论中所述,你是防火墙。 对于nginx,请检查其他答案。 对于Apache,请使用以下配置,不要忘记a2enmod proxy

#/etc/httpd/conf.d/node.conf
<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   ServerName mydomain
   ServerAlias www.mydomain.com
            
   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>
 
   <Location />
      ProxyPass http://127.0.0.1:3000
      ProxyPassReverse http://127.0.0.1:3000
   </Location>