Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express: How to serve font-awesome as static content?

I use express to serve static content on my site and I want to incorporate FontAwesome (npm install font-awesome). However in Font-Awesome's css the links to the font files are appended with a query-string containing versioning information witch express doesn't understand.

Has anyone encountered this and found a fix? Is there a simple way to make express ignore the qs for static content?

var express = require('express')
var app = express()

app.use('/static', express.static('./node_modules/font-awesome/css'))
app.use('/static', express.static('./node_modules/font-awesome/fonts'))

// response 200 | /static/font-awesome.min.css
// error 404    | /static/fontawesome--webfont.woff?v=4.6.3

Update As @Denys Séguret points our it's not the qs as I had thought. The actual request is for /fonts/fontawesome--webfont.woff?v=...

Solution

app.use('/fonts', express.static('./node_modules/font-awesome/fonts'))
like image 807
Aage Torleif Avatar asked Jul 25 '16 13:07

Aage Torleif


People also ask

How to serve a static file in express?

The argument you pass into express.static () is the name of the directory you want Express to serve files. Here, the public directory. In index.js, serve your static files below your PORT variable. Pass in your public directory as the argument:

How do I add Font Awesome fonts to my App?

Copy webfontsfolder from /node_modules/@fortawesome/font-awesometo the publicfolder in your main app directory. This will enable your application to serve Font Awesome fonts (icons) as static files from /webfontsurl of your application.

How to integrate Font Awesome with bootstrap Express?

cd fontawesome-bootstrap-express Install all dependencies yarn You should now have the basic Express app with Bootstrap installed and ready for further work to integrate Font Awesome. Test if the app installed correctly.

How do I add Font Awesome to CTR?

Install Font Awesome In your terminal window, if it is still running, teminate the app Ctr+cto go back to a command line and install a free version of Font Awesome icons: yarn add @fortawesome/fontawesome-free The Font Awesome assets will be installed in /node_modulesdirectory. Import styles to your Sass Bootstrap file


1 Answers

When your browser requests /static/fontawesome--webfont.woff?v=4.6.3, the server is free to ignore the ?v=xxx part. And that's what is done by the express.static module. The point of that part is to prevent browsers and proxies from using an old version of the file.

So the problem isn't where you think it is. The problem is you map the static route to two servers. The first one doesn't find the file and issues a 404.

(Dirty) Solution 1:

Change your mapping

app.use('/static', express.static('./node_modules/font-awesome'))

and change the URLs:

/static/fonts/fontawesome--webfont.woff?v=4.6.3

I say it's dirty because you're serving the unchecked content of a node modules (which is updated when you do a npm update). You should never do that.

 Solution 2:

Create a static directory (the name doesn't matter) and put inside the contents of the ./node_modules/font-awesome/css and ./node_modules/font-awesome/fonts directories then just map it using

app.use('/static', express.static('./static'));
like image 166
Denys Séguret Avatar answered Sep 25 '22 02:09

Denys Séguret