简体   繁体   English

node.js的内部服务器错误

[英]Internal server error for node.js

I have the following code to using node.js to serve a webpage in static/college.html But it hit the error and shows in browser "Internal server error: could not read file"which is the line printed in code for testing. 我有以下代码使用node.js来提供static / college.html中的网页但是它遇到错误并在浏览器中显示“内部服务器错误:无法读取文件”这是在代码中打印的用于测试的行。

Any suggestions and how to fix this? 有什么建议以及如何解决这个问题? Thanks 谢谢

// Require the functionality we need to use:
var http = require('http'),
        url = require('url'),
        path = require('path'),
        mime = require('mime'),
        path = require('path'),
        fs = require('fs');

// Make a simple fileserver for all of our static content.
// Everything underneath <STATIC DIRECTORY NAME> will be served.
var app = http.createServer(function(req, resp){
        var filename = path.join(__dirname, "static", url.parse(req.url).pathname);
        (fs.exists || path.exists)(filename, function(exists){
                if (exists) {
                        fs.readFile(filename, function(err, data){
                                if (err) {
                                        // File exists but is not readable (permissions issue?)
                                        resp.writeHead(500, {
                                                "Content-Type": "text/plain"
                                        });
                                        resp.write("Internal server error: could not read file");
                                        resp.end();
                                        return;
                                }

                                // File exists and is readable
                                var mimetype = mime.lookup(filename);
                                resp.writeHead(200, {
                                        "Content-Type": mimetype
                                });
                                resp.write(data);
                                resp.end();
                                return;
                        });
                }else{
                        // File does not exist
                        resp.writeHead(404, {
                                "Content-Type": "text/plain"
                        });
                        resp.write("Requested file not found: "+filename);
                        resp.end();
                        return;
                }
        });
});
app.listen(3456);

Thanks for the suggestions so far. 感谢您的建议到目前为止。 I just print this line url.parse(req.url).pathname but it shows empty there. 我只是打印这行url.parse(req.url).pathname但它在那里显示为空。 What could be wrong there? 有什么不对吗?

check for file name, make sure it's not a directory or null or a permissions issue. 检查文件名,确保它不是目录或null或权限问题。 add a proxy to trace the HTTP parameters going both ways. 添加代理以跟踪双向HTTP参数。 and run with phantomjs and hit the site with curl. 并使用phantomjs运行并使用curl命中该站点。

Add a Unit Test 添加单元测试

Most likely a permissions issue. 很可能是权限问题。 Try fixing the permissions on files. 尝试修复文件的权限。 Or run node as root 或以root身份运行节点

Why not use Express framework, it handles static folders for you. 为什么不使用Express框架,它会为您处理静态文件夹。

As well you should be using NginX to be handling static files in a scaleable way anyway. 您也应该使用NginX以可扩展的方式处理静态文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js:Mongoose 的内部服务器错误 - Node.js: Internal Server error with Mongoose 在Node.js中提供静态文件-500内部服务器错误 - Serving static files in node.js - 500 internal server error Windows Azure Node.js内部服务器错误 - Windows Azure Node.js Internal Server Error node.js内部消息服务器 - node.js internal message server 内部服务器错误节点js - Internal Server Error Node js index.js文件中的Azure Node.js内部服务器错误 - Azure Node.js Internal Server Error from index.js file 使用 Node.js 和 express.js 实现护照-facebook-token 时出现 500 内部服务器错误 - 500 Internal Server Error in passport-facebook-token implementation using Node.js and express.js 内部服务器错误500,在Azure中托管来自Visual Studio 2017的Node.js Express 4应用程序模板 - Internal server error 500, hosting Node.js Express 4 application template from Visual Studio 2017 in Azure 由于发生内部服务器错误,无法显示该页面。 Node.js 天蓝色 - The page cannot be displayed because an internal server error has occurred. Node.js azure 在Node.js中将内部服务器错误消息转换为客户端可读消息的最佳实践是什么? - What are some best practices for translating internal server error messages to client readable messages in Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM