Node.js不正确的path问题

当我做fs.readFile(file...我得到这个错误:

{[Error:ENOENT,open'foundation / css / normalize.css']

errno:34,

代码:'ENOENT',

path:'foundation / css / normalize.css'}

我是否必须路由与链接的HTML文件相关的path,或者与运行服务器和路由器的nod​​e.js文件的位置有关?

不,我不使用快递和“使用快递”不是一个可以接受的答案。


编辑

@ BenjaminGruenbaum最后的评论。 我不知道你的意思是我的HTTP响应,但我的代码逻辑如下:我加载我的index.html通过的URI,当它readFile文件时,它recursion通过其他所有链接的文件。 我有一个脚本将其分解,以正确显示正确的Content-Type 。 我会在这里包括它,因为它太长了评论:

 function route(handle, pathname, response, request){ // console.log("About to route a request for " + pathname); if (typeof handle[pathname] === "function"){ handle[pathname](response, request); } else { // console.log('NON HTML'); // console.log(pathname); var file_path = ""; var mimes = { 'css': 'text/css', 'js': 'text/javascript', 'htm': 'text/html', 'html': 'text/html', 'png': 'image/png', 'jpg': 'image/jpg', 'jpeg': 'image/jpeg' }; // parses the url request for a file and pulls the pathname var url_request = url.parse(request.url).pathname; // console.log('URL REQUEST'); // console.log(url_request); // finds the placement of '.' to determine the extension var tmp = url_request.lastIndexOf("."); // determines the extension by uing .substring that takes everything after '.' var extension = url_request.substring((tmp + 1)); // console.log('EXTENSION'); // console.log(extension); //set path of static pages if (extension === 'css' || extension === 'js' || extension === 'htm' || extension === 'html' || extension === 'png' || extension === 'jpg' || extension === 'jpeg'){ // console.log('LOAD STATIC PAGE'); file_path = url_request.replace("/", ""); // console.log(file_path); // console.log(); } //load needed pages and static files fs.readFile(file_path, function (error, data){ if(error){ // console.log("AM I DEAD"); // console.log(error); // console.log(); response.writeHeader(500, {"Content-Type": "text/html"}); response.write("<h1>FS READ FILE ERROR: Internal Server Error!</h1>"); } else{ response.writeHeader(200, {"Content-Type": mimes[extension]}); console.log('SHOULD BE SUCCESS') console.log(mimes[extension]); // console.log(data); response.write(data); } response.end(); }); response.end(); } } exports.route = route; 

正如你可以告诉我有多less地方我有console.log()我可以跟踪到底部它在这里失败:

 else{ response.writeHeader(200, {"Content-Type": mimes[extension]}); console.log('SHOULD BE SUCCESS') console.log(mimes[extension]); // console.log(data); response.write(data); } 

我为有些草率的缩进道歉。


最终编辑:

最终解决这个愚蠢的情况可以在我的下面的问题在这里find: Node.js – 资源解释为脚本,但与MIMEtypes文本/纯

非常感谢@BenjaminGruenbaum提出了一个Noob

您将不得不使用相对于您的NodeJS文件(更具体地说是入口点)的path,而不是您的客户端HTML。

这就像在bash脚本中一样,在Python中,在其他任何语言中都有一个理智的API,可以对操作系统中的本地文件系统function进行合理的调用。

实际上,客户端HTML与Node-in Node的path无关,它只是将文本发送到客户端的HTTP响应中。 它根本不知道服务器的内部文件系统。

(另外,我看不出有什么特别的东西可以帮忙呢)