[英]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:3000
和http://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.