简体   繁体   中英

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.

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. What could be wrong there?

check for file name, make sure it's not a directory or null or a permissions issue. add a proxy to trace the HTTP parameters going both ways. and run with phantomjs and hit the site with curl.

Add a Unit Test

Most likely a permissions issue. Try fixing the permissions on files. Or run node as root

Why not use Express framework, it handles static folders for you.

As well you should be using NginX to be handling static files in a scaleable way anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM