Skip to content

Instantly share code, notes, and snippets.

@philpoore
Created January 10, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpoore/6de35cb84ae353270f00c9d80d655751 to your computer and use it in GitHub Desktop.
Save philpoore/6de35cb84ae353270f00c9d80d655751 to your computer and use it in GitHub Desktop.
Shows error when res.json and next are both called.
% node middleware.js 130 ↵
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/usr/local/lib/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/usr/local/lib/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/usr/local/lib/node_modules/express/lib/response.js:249:15)
at /Users/phil/Desktop/middleware.js:20:6
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/local/lib/node_modules/express/lib/router/index.js:312:13)
at /usr/local/lib/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/usr/local/lib/node_modules/express/lib/router/index.js:330:12)
at next (/usr/local/lib/node_modules/express/lib/router/index.js:271:10)
'use strict';
const express = require('express');
const app = express();
const somethingThatFailed = false;
app.use((req, res, next) => {
if (somethingThatFailed){
console.log("Error here");
}else{
res.json({A: 1});
}
next();
});
app.use((req, res, next) => {
res.json({A: 2});
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment