简体   繁体   English

将node.js应用程序指向我的IP地址并在本地运行

[英]pointing node.js app to my ip address and running locally

So I have two questions about the topic. 因此,我对该主题有两个问题。 I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start. 我已经通过digitalocean.com购买了一个VPS来托管我的node.js应用程序,他们给了我一个示例hello world应用程序来启动。

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080 因此,此代码可在本地使用,当我在VPS上托管时,我用VPS的IP地址替换了“ localhost”,当我访问http:// IP_Address_of_VPS:8080时,它就可以在网络上使用

My first question: How do I host it live on the web but have it point to my IP address? 我的第一个问题:如何在网络上实时托管它,但它指向我的IP地址? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080 当我用我的IP地址替换“ localhost”时,当我转到http:// my_IP_Address:8080时它不起作用

My second question: I made a test app following a node.js tutorial and here is the code 我的第二个问题:我按照一个node.js教程制作了一个测试应用,这是代码

var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);

app.use(express.static(__dirname + '/public'));

http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});

Where public is the folder with a simple html page. 其中public是带有简单html页面的文件夹。 This app works when I run it locally at http://localhost:8080 当我在http:// localhost:8080本地运行它时,此应用程序可以工作

Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? 当我未在http.listen()函数中指定“ localhost”时,有人可以解释第二个应用程序如何在本地运行吗? Also how do I point the second app to run from my IP address if the process is any different from the first? 另外,如果过程与第一个应用程序有所不同,如何将第二个应用程序指向从我的IP地址运行?

First question 第一个问题

I hope I understood your question right, here is an attempt at an answer :) 希望我对您的问题理解正确,这里是一个答案的尝试:)

Depends a bit on what you mean with 'my ip address'. 取决于您对“我的IP地址”的含义。 If it is your public ip adress, you might have to set up port forwarding. 如果这是您的公共IP地址,则可能必须设置端口转发。 Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default. 默认情况下,大多数路由器都会拒绝从外部网络(连接到自己的公共IP地址时所进行的操作)到内部网络的流量。

If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding. 如果您指的是专用ip地址(本地局域网),则需要使用公用ip地址并设置端口转发。

When you set up port forwarding to your local machine you might also want to make your private ip address static. 当您设置端口转发到本地计算机时,您可能还希望将私有IP地址设为静态。

However, I do not recommend using your home computer as a production server. 但是,我不建议您将家用计算机用作生产服务器。 You will need to solve a lot of problems like making your network secure and having low downtime. 您将需要解决许多问题,例如使网络安全并减少停机时间。

Second question 第二个问题

When no hostname is passed to http.listen , the server will accept connections on all adresses. 如果没有将主机名传递给http.listen ,则服务器将接受所有地址的连接。 See documentation for more information . 有关更多信息,请参见文档

This works, because when you omit arguments in a function call, they will default to undefined . 这行得通,因为当您在函数调用中省略参数时,它们将默认为undefined For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined) . 例如, http.listen(8080)可能显示为http.listen(8080, undefined, undefined, undefined) Inside the function body these will often be substituted with some default value. 在函数体内,这些通常会被一些默认值代替。

The process of making your app available to the rest of the world should not be any different. 将您的应用程序提供给世界其他地方的过程应该没有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM