无法提供字体文件

时间:2019-01-06 04:34:44

标签: node.js mime-types restify

我将Node用作服务器,并尝试提供以下扩展名的字体文件:woff2,woff,ttf。我正在使用restify

var ServeFile = (folder: string, page: string, res: Restify.Response) => {
    let path:string = __dirname+"/"+folder+page;
    let MimeType: string | null = Mime.getType(path);
    if(!MimeType) MimeType = "";
    let body:string = fs.readFileSync(path, "utf8");
    // if(MimeType == "font/woff2"){
    //     MimeType = "application/font-woff2";
    // }
    if(MimeType.startsWith('image') || MimeType.startsWith('font') || MimeType.startsWith('application')){
        res.writeHead(200, {
            'Content-Length': Buffer.byteLength(body, "binary"),
            'Content-Type': MimeType
        });
        res.write(body,'binary');
    }else{
        res.writeHead(200, {
            'Content-Length': Buffer.byteLength(body, "utf8"),
            'Content-Type': MimeType
        });
        res.write(body);
    }
    res.end();
}

尝试时,Chrome表示无法解码下载的字体。

对于每种类型,它还会发出以下警告:

OTS parsing error: Failed to convert WOFF 2.0 font to SFNT
OTS parsing error: incorrect file size in WOFF header
OTS parsing error: FFTM: misaligned table

我在做什么错了?

0 个答案:

没有答案