NodeJS:本地主机一直在加载

3

我正在使用NodeJS来提供一些文件,但页面一直在加载。这是我的index.js文件。

const express = require("express");
const path = require("path");
const http = require("http");
const socketio = require("socket.io");

const app = express();
const PORT = process.env.PORT || 3000;

const server = http.createServer(app);
const io = socketio(server);

app.use(express.static(path.join(__dirname, "../public")));
server.listen(PORT, () => {
  console.log("server listening to port: " + PORT);
});

这是我的public/index.html。
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/styles.css">
</head>

<body>
    <div id="media-video">
        <video id="media-camera" autoplay="autoplay" playsinline>

        </video>
        <video id="media-remote-video" autoplay="autoplay" playsinline>

        </video>
        <video id="media-screen-capture" autoplay="autoplay" playsinline>

        </video>
        <select name="video" id="media-video-devices">
            <option value="None">None</option>
        </select>
        <select name="audio" id="media-audio-devices">
            <option value="None">None</option>
        </select>
        <button id="start-media-stream">Start Stream</button>
        <button id="end-media-stream">End Stream</button>
        <button id="start-media-screen-capture">Capture Screen</button>
        <button id="end-media-screen-capture">End screen capture</button>
    </div>
    <script src="/socket.io/socket.io.js"></script>
    <script src="/js/index.js"></script>
</body>

</html>
         

页面加载完全,包括所有JS和CSS文件,但是localhost:3000仍在加载中。

尝试使用app.listen而不是server.listen - undefined
我想要使用socket.io,所以我在服务器上监听而不是应用程序。 - undefined
你的代码看起来是正确的,我在本地重新创建了一下,它对我来说可以加载。也许你有一些奇怪的进程在3000端口监听。你检查过 ps -a | grep node 了吗? - undefined
但是你仍然需要给用户返回一个响应,否则服务器会一直挂起直到超时。 - undefined
@uke 我尝试了那个命令,但没有返回任何结果。 - undefined
2个回答

0
Express在创建套接字服务器时会同时创建一个Web服务器,它们都从同一端口提供服务,本例中为3000。要连接到WebSocket,您需要使用socket.io连接到"ws://localhost:3000",而不是使用任何HTTP请求。

问题不在于socket.io或者web服务器和socket服务器。即使所有文件,如html、JS和css文件都加载完成,页面仍然持续加载。 - undefined

0
页面“挂起”是因为您没有向用户发送响应。代码只是提供中间件express静态文件,但您没有处理请求/响应周期...
这是一个示例,您可以这样做:
// require http native node api...

var http = require('http');

// initialize express...here the server is not listening
// your files are not served yet...

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

// this is the middleware that serves your static files
// the css and js come from this...

app.use(express.static(path.join(__dirname, "../public")));

// now you create the server using the HTTP module
// passing express as function and the PORT you have set...
// this is listening, not handling your request/response cycle...

http.createServer(app).listen(PORT);

// this is an example of how you end a request response cycle 
// by sending a file back, it could be other things...

app.get('/', function(req, res) {
    res.sendFile("index.js");       
});

但是我想提供一个包含index.html、js和css文件的整个目录,这就是为什么我选择使用express.static()。 - undefined
这里有一些混淆。让我来澄清一下。Express Statis用于提供静态文件,但它不处理请求响应周期,除非你设置它这样做。你仍然需要在代码中使用它,但你需要通过向客户端发送响应来结束响应周期。在我的例子中,你正在发送一个名为index.js的文件,但也可以是其他东西。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接