简体   繁体   中英

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?

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);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM