Pranay kumar
Pranay kumar

Reputation: 2197

Not able to access static files in express

I tried to use my fonts folder files inside one of my controller files. fonts folder is inside the public folder. I checked some of the StackOverflow questions and add this code to my app.js file. but somehow it didn't work for me.

app.use(express.static(__dirname + '/public'));

here is the controller file code in which I try to access these files.

var fonts = {
  Roboto: {
    normal: '/fonts/Roboto-Regular.ttf',
  }
};

var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);

This is the error I got when I run the server.

Error: ENOENT: no such file or directory, open '/fonts/Roboto-Regular.ttf'

Upvotes: 0

Views: 245

Answers (1)

Quentin
Quentin

Reputation: 943207

A static end point is only going to apply when clients read URLs from the server. It has nothing to do with other server-side JS that reads directly from the filesystem.

You need to use an absolute path (which you could compute relative to the directory the JS file is in with __dirname just like you did when setting up the static end point.

Upvotes: 1

Related Questions