0

Trying to get a site using Express.js I'm working to display a pdf on the site. Attempting to use fs and path to get it showing, and I'm really really having trouble as it seems the the path callbacks I am using are being used incorrectly, hence I'm getting an error. Frankly, this code is written up through parsing through dozens of SO posts so I'm struggling to see why I'm receiving this error message (The argument 'path' must be a string or Uint8Array without null bytes).

I've tried changing the fs.readfileSync to fs.statSync. Prior to using path, I had the link set up with fs alone however the code was lengthy as I was struggling to get a relative path sorted out when linking to the pdf in fs.

const express = require("express"),
      fs      = require("fs"),
      path    = require("path");

const router = express.Router();

router.get("/bob", (req,res)=> {
    var filePath = path.join(__dirname, "..", "views", "files", "bob.pdf");
    var fileString = fs.readFileSync(filePath, "utf-8");
    fs.readFile(fileString , (err,data) => {
        if(err){
            console.log(err);
        } else {
            res.contentType("application/pdf");
            res.send(data); 
        }
    });
});

I expect the code to encode the pdf and display it, however I am getting the error message: "The argument 'path' must be a string or Uint8Array without null bytes. Received '%PDF-1.7....'."

brownmamba
  • 13
  • 2
  • 8

0 Answers0