简体   繁体   中英

Can't set headers after they are sent error from Express middleware

I have a Node Express app and have written a bit of middleware to do the following:

  • Check all incoming requests if it's from a bot
  • Allow requests for raw resources through
  • If requests are from a bot, server an HTML snapshot

I'm getting the following error from the code below:

Error: Can't set headers after they are sent.

I'm not sure why this is happening, can anyone help?

var snapshotDir = require("path").resolve(__dirname + "/../snapshots");

var bots = [
  /facebookexternalhit/,
  /googlebot/
];

var resources = [
  /\/framework\//,
  /\/partials\//,
  /\/views\//
];

app.use(function(req, res, next){ 

  //test user-agent against bots array
  var isBot = function(agent){
    return bots.some(function(bot){
      return bot.test(new RegExp(agent));
    });
  }

  //test path for raw resources
  var isResource = function(path){
    return resources.some(function(resource){
      return resource.test(new RegExp(path));
    });
  }

  //check request type
  if (isResource(req.url)) return next(); //request is for raw resource
  if (!isBot(req.get("user-agent")) && !/\?_escaped_fragment_=/.test(req.url)) return next(); //user-agent is not bot

  //get url path without escaped fragment
  var path = req.url.replace("?_escaped_fragment_=", "");

  //format path into filename
  if (path.charAt(0) !== "/") path = "/" + path; //prepend fragment with '/'
  if (path === "/") path = "/index.html"; //home requested: serve index.html
  if (path.indexOf(".html") == -1) path += ".html"; //append fragment with '.html'

  //serve snapshot file
  try { res.sendFile(snapshotDir + path); } //serve html snapshot
  catch (err) { res.send(404); } //no snapshot available, serve 404 error

  //next request
  return next();

});

You can't write twice res.send for one request (which you are doing here if the first statement fails and goes to the catch block)

 try { res.sendFile(snapshotDir + path); } //serve html snapshot
 catch (err) { res.send(404); } //no snapshot available, serve 404 error

You should try to understand why the error occurs, then do a check yourself before sending the file.

Probably the file doesn't exist (you can check that by using fs.statSync

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