Search code examples
javascriptnode.jscontent-typejson-server

Change json-server response type for custom route. NotAcceptableError: Not Acceptable


I am using json-server custom output to read md file. I want to return the contend on specific rout as text/plain, so I wrote this code:

server.get('/static/*', (req, res) => {

  const filePath = path.join(__dirname, `myfile.md`);
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      res.send(err);
      return;
    }
    res.format({
     'content-type: text/plain': () => res.send(data),
    });
  });
});

When I am accessing that route I am getting an error NotAcceptableError: Not Acceptable Any ideas how to fix?


Solution

  • res.format is not what you want to use. You use res.set to set a response header - like so:

    server.get('/static/*', (req, res) => {
        const filePath = path.join(__dirname, `myfile.md`);
        fs.readFile(filePath, 'utf8', (err, data) => {
            if (err) {
                res.send(err);
                return;
            }
            res.set('Content-Type', 'text/plain');
            res.send(data);
        });
    });