0

I have a simple node server and I have a CSS file that requests a font from my server:

@font-face{
     font-family: 'NiagaraSolid-Reg';
     src: url('http://localhost:1111/NIAGSOL.TTF');
}

This is what I attempted to do. It normally works with .html, .css, and .js files:

http.createServer(function(request,response){
    var arguments=request.url.slice(1).split("/");
    switch(arguments[0]){
        case "NIAGSOL.TTF":
            response.end(fs.readFileSync("website/NIAGSOL.TTF").toString());
            break;
        //etc 
})

But when I do this, in Chrome I get the error message: Failed to decode downloaded font: http://localhost:1111/NIAGSOL.TTF

In searching for an answer, everything I came across was too complicated for me to understand becuase it required prerequisite knowledge of a lot of things. I'm completely illiterate when it comes to buffers and streams and binaries and encodings etc. I need a simple "explain-like-I'm-five" answer for how to serve a .tff file.

Thanks!

Nick Manning
  • 2,828
  • 1
  • 29
  • 50

2 Answers2

0

How to serve an image using nodejs

This link helped me figure out what to do. On a whim I decided to try response.end(fs.readFileSync("website/NIAGSOL.TTF"),"binary"); and it worked. Not sure why exactly but hey!

Community
  • 1
  • 1
Nick Manning
  • 2,828
  • 1
  • 29
  • 50
0

If you're coding a raw http server in node, you must set the "Content-Type" header in the response. So if you're serving an html file you set the header content type to "text/html". Likewise you must tell node to serve .ttf files as such, by setting the header on the response thats serving the ttf file. You can do so by setting the content type to "application/x-font-ttf".

http.createServer(function (req, res) { res.writeHead(400, { "Content-Type": "application/x-font-ttf" }); }); You'll need to write you're own code for handling the different file types you're server may be serving. Just remember that if its a .ttf, set the header as above.

OB7DEV
  • 143
  • 1
  • 9