简体   繁体   English

如何安装node.js从我的家中广播

[英]How to Install node.js to broadcast from my home

I am studying Node.js presently and wonder if it is possible to broadcast my sites from one of my home PCs. 我目前正在研究Node.js,想知道是否可以从一台家用PC广播我的站点。

For local publishing I am playing with this code: 对于本地发布,我正在使用以下代码:

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

fs.readFile('index.html', (err, html) => {
    if(err){
        throw err;
    };

    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-type','text/html');
        res.write(html);
        res.end();
    });

    server.listen(port, hostname, () => {
        console.log('Server started on port: '+port);
    });
});

But I am not sure what needs to be done to make my sites available to the public. 但是我不确定要使我的网站对公众可用需要做些什么。 How do I set up the "hostname" and whatever else needs to be done to broadcast one or more sites. 如何设置“主机名”以及广播一个或多个站点所需执行的其他任何操作。

I am not asking for a complete robust server code, just a minimal site that says Hello on the WEB. 我不是在要求完整的可靠服务器代码,而只是在Web上说“ Hello”的最小站点。

Many thanks 非常感谢

Easy Route: See Arun's answer. 简易路线:请参阅阿伦的答案。

Formal route: 正式路线:

  1. Buy a domain name (sounds like you have some) 买一个域名(听起来有点像)
  2. Contact your ISP to get it pointed to a public IP, 请与您的ISP联系,以使其指向公共IP,
  3. Get a server to have the public IP 获取服务器以拥有公共IP
  4. Point that server to your local machine's ip:port. 将该服务器指向本地计算机的ip:port。
  5. If you have node running and listening on that port it should work. 如果您的节点正在运行并且正在该端口上侦听,则它应该可以工作。

Informal Route: (Skips to step 3 above) 非正式路线:(跳至上述第3步)

  1. Go to your router's configuration page and find the public IP. 转到路由器的配置页面并找到公共IP。
  2. While at the configuration page set up port forwarding. 在配置页面上设置端口转发。 (This directs traffic to the public ip to your local machine's port that you're listening on.) (这会将到公共ip的流量定向到您正在监听的本地计算机的端口。)
  3. should be able to access that page via the public IP. 应该能够通过公共IP访问该页面。 (assuming firewalls are set to open those ports too). (假设防火墙也设置为打开这些端口)。

Anyone feel free to correct me/add detail to the steps. 任何人都可以随意纠正我/在步骤中添加细节。

you can host it in platforms like Heroku, firebase etc. they give you a url which is publicly accessible. 您可以将其托管在Heroku,firebase等平台上。它们会为您提供可公开访问的网址。

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

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

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