Nodejs中favicon.ico导致报错

Nodejs中favicon.ico导致报错

var http = require(“http”);
var url = require(“url”);

function start(route,handle){ function onRequest (request,response){ var pathname = url.parse(request.url).pathname; var content; var postdata;

console.log(pathname); response.writeHead(200,{“Content-Type”:“text/html”}); request.setEncoding(“utf8”); request.addListener(“data”,function(Chunk){ postdata += postDataChunk; }); request.addListener(“end”,function(){ console.log(“received post data end”); content = route(handle,pathname); }); response.write(content); response.end(); }

http.createServer(onRequest).listen(8080); }

exports.start = start;

我代码是这样的,报错

throw new TypeError('first argument must be a string or Buffer');

貌似是由于浏览器请求的 /favicon.ico 导致错误,请问怎么解决呀


6 回复

要解决 favicon.ico 请求导致的报错问题,我们可以通过检查请求路径来避免对非预期路径的处理。在你的代码中,当浏览器请求 /favicon.ico 时,可能会引发未预料到的错误,因为这段路径没有被正确处理。

以下是改进后的代码,增加了对 /favicon.ico 请求的处理逻辑:

var http = require("http");
var url = require("url");

function start(route, handle) {
    function onRequest(request, response) {
        var pathname = url.parse(request.url).pathname;

        console.log("Request for " + pathname + " received.");

        // 检查是否是/favicon.ico请求
        if (pathname === '/favicon.ico') {
            response.writeHead(200, { 'Content-Type': 'image/x-icon' });
            response.end();  // 结束响应
            return;  // 阻止后续代码执行
        }

        var content;
        var postdata = '';

        request.setEncoding("utf8");
        request.addListener("data", function (chunk) {
            postdata += chunk;  // 注意变量名修正
        });

        request.addListener("end", function () {
            console.log("Received POST data end");
            content = route(handle, pathname);
            response.writeHead(200, { "Content-Type": "text/html" });
            response.write(content);
            response.end();
        });
    }

    http.createServer(onRequest).listen(8080);

    console.log("Server has started.");
}

exports.start = start;

解释

  1. 检查请求路径:增加了一段代码来检查请求路径是否为 /favicon.ico
  2. 返回空响应:如果路径匹配 /favicon.ico,则设置响应状态码为 200,并设置正确的 Content-Typeimage/x-icon,然后结束响应。
  3. 阻止后续代码执行:使用 return 语句防止后续的处理逻辑被执行,从而避免不必要的错误。

通过这种方式,你可以优雅地处理浏览器自动发送的 favicon.ico 请求,避免因这些请求引发的错误。


All errors. can’t fix. you must rewrite.


签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

怎么解决呢?

很简单,如果检测到url为/favicon.ico,则直接response.end();

look the framework : https://github.com/brighthas/stuwebfk

at /lib dir source.


签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

在你的Node.js代码中,当浏览器请求/favicon.ico时,服务器试图将一个未定义的变量写入响应流,这会导致错误。这是因为route(handle, pathname)返回的内容可能没有被正确地赋值给content

解决方案

你可以通过检查请求路径来避免处理/favicon.ico请求。另外,确保route(handle, pathname)函数正确返回内容,并且在所有情况下都能赋值给content

以下是修改后的代码示例:

var http = require("http");
var url = require("url");

function start(route, handle) {
    function onRequest(request, response) {
        var pathname = url.parse(request.url).pathname;
        var content;
        var postdata = "";

        console.log(pathname);

        if (pathname === "/favicon.ico") {
            // 忽略 favicon 请求
            return;
        }

        response.writeHead(200, { "Content-Type": "text/html" });

        request.setEncoding("utf8");
        request.on("data", function (chunk) {
            postdata += chunk; // 注意这里是 `chunk` 而不是 `postDataChunk`
        });
        request.on("end", function () {
            console.log("received post data end");
            content = route(handle, pathname);
            response.write(content);
            response.end();
        });
    }

    http.createServer(onRequest).listen(8080);
}

exports.start = start;

关键点解释

  1. 忽略 favicon.ico 请求

    • 添加了条件判断if (pathname === "/favicon.ico"),当路径为/favicon.ico时直接返回,不进行后续处理。
  2. 修正拼写错误

    • postdata += postDataChunk; 改为postdata += chunk;,这里chunk是正确的变量名。
  3. 确保数据赋值

    • 确保在所有情况下content都有值,避免尝试向响应流写入undefined

这样可以避免因为favicon.ico请求导致的错误,并且保证其他请求正常处理。

回到顶部