为什么 Node.js 无法提供 .woff 文件

2023-12-21

我下载了.woff由于中国的某些网络原因,来自 Google 网络字体的文件。之前我尝试过@font-face那个在Github 页面 http://jiyinyiyong.github.com/she/bin/它有效。但这一次我花了一个小时才找到哪里坏了。

我使用 Node 来提供静态文件mime,以及content-type似乎application/x-font-woff,以及我在 CoffeeScript 中的代码:

exports.read = (url, res) ->
  filepath = path.join __dirname, '../', url
  if fs.existsSync filepath
    file_content = fs.readFileSync filepath, 'utf8'
    show (mime.lookup url)
    res.writeHead 200, 'content-type': (mime.lookup url)
    res.end file_content
  else
    res.writeHead 404
    res.end()

As the content-type of .woff在 Github Pages 上是application/octet-stream,我只是在代码中注释掉该行以使其相同..但它仍然失败:

exports.read = (url, res) ->
  filepath = path.join __dirname, '../', url
  if fs.existsSync filepath
    file_content = fs.readFileSync filepath, 'utf8'
    show (mime.lookup url)
    # res.writeHead 200, 'content-type': (mime.lookup url)
    res.end file_content
  else
    res.writeHead 404
    res.end()

最后,我切换到Nginx服务器来服务.woff文件..终于开始工作了。

但是如何在 Node 上解决这个问题呢?


在这一行中,fs.readFileSync(filepath, 'utf8')编码设置为'utf8'。它需要是'binary'.

另外,res.end(file_content)函数需要传递正确的编码。尝试res.end(file_content, 'binary').

我遇到了同样的问题,必须自己解决,这个答案似乎在网上任何地方都不存在。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Node.js 无法提供 .woff 文件 的相关文章

随机推荐