简体   繁体   English

使用服务器而不是本地机器时,我应该用什么替换“http://localhost:3000”?

[英]What do I replace "http://localhost:3000" with when using a server and not local machine?

I've been doing a lot of online courses with node and express.我一直在用 node 和 express 做很多在线课程。 I want to get sockets.io to work but I can't even establish a connection at the moment.我想让 sockets.io 工作,但目前我什至无法建立连接。 I am using a cPanel virtual private server and running code in the server terminal and then trying to use a website hosted on the server to access the .js file running on the server.我正在使用 cPanel 虚拟专用服务器并在服务器终端中运行代码,然后尝试使用服务器上托管的网站来访问服务器上运行的 .js 文件。

I've tried all sorts of different things but I'm reducing it to its most basic level to try get a connection.我尝试了各种不同的方法,但我将其降低到最基本的级别以尝试建立连接。 All the videos I've seen are running on a local machine and using the command prompt on a local machine to run the .js file and the browser to access http://localhost:3000 .我看到的所有视频都在本地机器上运行,并使用本地机器上的命令提示符运行.js文件和浏览器访问http://localhost:3000

The .js file I'm running on my cPanel server looks like this;我在 cPanel 服务器上运行的.js文件如下所示;

var express = require('express');
var app = express();

app.get('/', function(req,res){
    res.send('Hello world 2');
})

app.listen(3000);

So how do I then access that via the browser?那么我如何通过浏览器访问它呢? I have tried http://mywebsite.com:3000 and http://11.22.33.444:3000 if 11.22.33.444 is the server ip, but the browser just times out and there is no output in the server console.如果 11.22.33.444 是服务器 ip,我已经尝试过http://mywebsite.com:3000http://11.22.33.444:3000 ,但浏览器只是超时并且服务器控制台中没有输出。

ultimately I need to run a socket.io command that looks like this;最终我需要运行一个看起来像这样的 socket.io 命令;

var socket = io.connect('http://localhost:3000');

and in all the tutorials I've seen they use this localhost:3000 but no one explains how to access this if its on an actual server so I'm pretty lost.在我见过的所有教程中,他们都使用这个 localhost:3000 但没有人解释如何访问它,如果它在实际服务器上,所以我很迷茫。

There are other examples like;还有其他例子,如;

...
const http = require('http').createServer();
...
http.listen(3000 => () => {
  console.log('listening on port 3000');
});

That's just a snippet of the code but I'm wondering how I then access that 3000 port from the browser without http://localhost:3000这只是代码片段,但我想知道如何在没有http://localhost:3000情况下从浏览器访问该 3000 端口

IF you read the docs you will see that there is a guide how to connect it with express: https://socket.io/docs/如果您阅读文档,您会看到有一个指南如何将其与 express 连接: https : //socket.io/docs/

 var app = require('express')();
 var server = require('http').Server(app);
 var io = require('socket.io')(server);

 server.listen(3000);
 // WARNING: app.listen(3000) will NOT work here!

 app.get('/', function (req, res) {
   res.status(200).json({ message: "Connected" });
 });

 io.on('connection', function (socket) {
  console.log("somebody connected");
 });

Think I just solved it.想我刚刚解决了它。 I tried a different port and it worked :/我尝试了一个不同的端口,它起作用了:/

No need to specify any address in io.connect()无需在 io.connect() 中指定任何地址

 const app = express(); const http = require('http').Server(app); const io = require('socket.io')(http); http.listen(process.env.PORT || 3000, function() { });
 <script src="/socket.io/socket.io.js"></script> var socket = io.connect();

暂无
暂无

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

相关问题 为什么我得到“http://localhost:3000/”? 在反应中使用useNavigate时的路线? - Why am I getting 'http://localhost:3000/?' route when using useNavigate in react? 当我尝试打开http:// localhost:3000 / pages / home时出错 - Error when I try to open http://localhost:3000/pages/home 尝试将数据从客户端发送到服务器时,POST http://localhost:3000/data 400(错误请求) - POST http://localhost:3000/data 400 (Bad Request) when trying to send data from client to server 在本地服务器上使用$ http.get()时,为什么总是不断找不到404? - Why do I keep getting a 404 not found when using $http.get() on local server? POST http:// localhost:3000 / upload 500(内部服务器错误) - POST http://localhost:3000/upload 500 (Internal Server Error) 当我想将 localhost:4000 重定向到 localhost:3000 时出现错误 CORS - Error CORS when i want redirecting localhost:4000 to localhost:3000 当 react 应用程序未在 localhost:3000 上加载时,如何设置 webpack 和 babel - How do I set up webpack and babel when react app is not loading on localhost:3000 如何在反应中将 localhost:3000 更改为 custom.domain - How do I change localhost:3000 to custom.domain in react 我如何将 http post 请求从 localhost:5000 发送到 localhost:3000 - how can i send http post request from localhost:5000 to localhost:3000 为什么我不能在反应中使用 axios 从 localhost:3000 调用 localhost localhost:5000 - Why can't I call localhost localhost:5000 from localhost:3000 using axios in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM