节点.js我做错了哪一部分?服务器.js正在运行,但我看不到 HTML 页面

Node.js Which part i did wrong? server.js is running, but i cant see the html page

本文关键字:js 运行 我看 看不到 页面 HTML 服务器 错了 一部分 节点      更新时间:2023-09-26

这是我到目前为止所拥有的,我正在腻子中运行它,

var http = require('http');
var fs = require('fs');
const POST =8080
http.createServer(function(request, response) {
    var url = request.url;
    switch(url) {
    case '/':
        console.log('1');
        getStaticFileContent(response,'templates/Assignment1.html','text/html');
        break;
    case'/new':
        console.log('2');
        getStaticFileContent(response,'templates/new.html','text/html');
        break;
    default:
        response.writeHead(404,{'Content-Type':'text/plain'});
        response.end('404 - Page not Found.');
     }
}).listen(POST)
console.log('Server is running');
function getStaticFileContent(response, filepath, contentType) {
    fs.readFile(filepath, function(error, data) {
        if (error) {
            response.writeHead(500,{'Content-Type': 'text/plain'});
            response.end('500 - Internal Server Error.');
        }
        if (data) {
            response.writeHead(200,{'Content-Type': 'text/html'});
            response.end(data);
        }
    });
}

当我在这种情况下添加这些控制台.log("1")时,会弹出这些错误。

Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Object.<anonymous> (/net/files.cis.ksu.edu/exports/home/y/yuex/CIS526/server.js:21:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

有人可以告诉我我哪里做错了吗?

问题是服务器正在侦听同一端口,该端口已被另一个应用程序占用。释放端口,或尝试侦听另一个端口。其余代码似乎还可以。

运行此 linux 命令以检查您正在侦听的端口是否已被占用:

netstat -an | grep <port number>

netstat -aon | more