簡體   English   中英

node.js的內部服務器錯誤

[英]Internal server error for node.js

我有以下代碼使用node.js來提供static / college.html中的網頁但是它遇到錯誤並在瀏覽器中顯示“內部服務器錯誤:無法讀取文件”這是在代碼中打印的用於測試的行。

有什么建議以及如何解決這個問題? 謝謝

// 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);

感謝您的建議到目前為止。 我只是打印這行url.parse(req.url).pathname但它在那里顯示為空。 有什么不對嗎?

檢查文件名,確保它不是目錄或null或權限問題。 添加代理以跟蹤雙向HTTP參數。 並使用phantomjs運行並使用curl命中該站點。

添加單元測試

很可能是權限問題。 嘗試修復文件的權限。 或以root身份運行節點

為什么不使用Express框架,它會為您處理靜態文件夾。

您也應該使用NginX以可擴展的方式處理靜態文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM