(NodeJS)Http服务器不会从文件夹加载JS / CSS文件

美好的一天 ,

我的NodeJS http服务器有一个小问题。 服务器不会从文件夹加载CSS / JS文件,只能从url,我不知道为什么。 如果有人能花一些时间,给我一些关于错误的提示,我将不胜感激。

这是服务器代码:

var http = require("http"); var gs = require("querystring"); var url = require("url"); var fs = require("fs"); var server = http.createServer(function (request, response, err) { //HTML if (request.url === "/") { sendFileContent(response, "HTML/Login.html", "text/html"); console.log("Requested URL : " + request.url + "\n"); } else if (request.url === "/main") { sendFileContent(response, "HTML/Main_Home.html", "text/html"); console.log("Requested URL : " + request.url + "\n"); } // JS / CSS / Other formats else if (/^\/[a-zA-Z0-9\/]*.js$/.test(request.url.toString(1))) { sendFileContent(response, request.url.toString().substring(1), "text/javascript"); } else if (/^\/[a-zA-Z0-9\/]*.css$/.test(request.url.toString())) { sendFileContent(response, request.url.toString().substring(1), "text/css"); } else if (/^\/[a-zA-Z0-9\/]*.json$/.test(request.url.toString())) { sendFileContent(response, request.url.toString().substring(1), "application/json"); } else if (/^\/[a-zA-Z0-9\/]*.ts$/.test(request.url.toString())) { sendFileContent(response, request.url.toString().substring(1), "text/javascript"); } else if (/^\/[a-zA-Z0-9\/]*.png$/.test(request.url.toString())) { sendFileContent(response, request.url.toString().substring(1), "image/png"); } else if (/^\/[a-zA-Z0-9\/]*.jpg$/.test(request.url.toString())) { sendFileContent(response, request.url.toString().substring(1), "image/jpeg"); } else { console.log("Requested URL : " + request.url + "\n"); response.end(); } }); server.listen(1337, function () { require("console-stamp")(console, '[HH:MM:ss]'); console.log("HTTP Server runs on port : 1337"); }); console.log("Server ready...."); 

这里是Send file contentfunction:

 function sendFileContent(response, fileName, contentType){ fs.readFile(fileName, function (err, data) { if (err) { response.writeHead(404); response.end("Not Found!"); } else { response.writeHead(200, { "Content-Type": contentType }); response.end(data); } }); }; 

这就是我如何在HTML中调用文件

 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link type="text/css" rel="stylesheet" href="../materialize/css/materialize.min.css" media="screen,projection" /> <link type="text/css" rel="stylesheet" href="../CSS/App.css" /> <title></title> <script type="text/javascript" src="../jquery/jquery-3.2.0.min.js"></script> <script type="text/javascript" src="../materialize/js/materialize.min.js"> </script> <script type="text/javascript" src="../Javascript/Main_App.js"></script> 

感谢您的时间 !

你正试图实现你自己的静态文件服务器,并且在你的实现中还有很多问题,除了那些你问到的问题之外,我认为你需要重新思考你的方法。

要提供静态文件,使用像express.static这样的工作解决scheme要容易得多 – 但是如果您真的需要,可以在下面阅读没有Express的解决scheme。 使用express.static示例:

 var path = require('path'); var express = require('express'); var app = express(); var dir = path.join(__dirname, 'public'); app.use(express.static(dir)); app.listen(3000, function () { console.log('Listening on http://localhost:3000/'); }); 

使用connect,express,http和net查看更多选项的答案:

  • 如何使用nodejs提供图像

在这个答案中有一些使用express.static ,express, express.static ,使用connect ,使用http和使用net ,甚至使用原始TCP套接字的版本来做这里你要做的事情的例子并不像你的代码在这里。 你可能想看看它,并在这些例子的基础上你的代码。