express icon indicating copy to clipboard operation
express copied to clipboard

After update to 2.3.0, GET route being matched by POST

Open publickeating opened this issue 15 years ago • 12 comments

I have the following routes:

app.post('/session’,
  sc.createSession,
  sc.returnSession);
app.get('/session',
  sc.checkSession,
  sc.returnSession);

After updating, a POST to /session is getting caught by the GET route. Downgrading back to 2.2.2 fixes it. My config is something like this:

app.use(express.logger());
app.use(express.bodyParser());
app.use(cookieSessions());
app.use(form());
app.use(express.static(__dirname + '/public’));
app.set('views', __dirname + '/views’);

If I remove the app.get(‘/session’) route, than no other route matches the POST.

publickeating avatar Apr 26 '11 20:04 publickeating

hmm yeah that does not sound right... we've been using 2.3.0 and havent had any issues

tj avatar Apr 26 '11 20:04 tj

if possible a small script to reproduce the issue stand-alone or as a test would be great

tj avatar Apr 26 '11 20:04 tj

I will try to expand on it when I get the time. Thanks.

publickeating avatar Apr 26 '11 20:04 publickeating

I found how to make it work in 2.3.0, perhaps I’m not using exports properly:

app.post('/session’,
  sc.createSession,
  sc.returnNewSession); // Don’t re-use the same function
app.get('/session’,
  sc.checkSession,
  sc.returnSession);

Where:

sc = require(‘./sessions_controller’)

And in sessions_controller.js:

exports.returnSession = function(req, res, next) { … };
exports.returnNewSession = function(req, res, next) { identical to returnSession … };

publickeating avatar Apr 26 '11 21:04 publickeating

interesting, I'll try and add a test case

tj avatar Apr 26 '11 21:04 tj

seems fine to me with the same function

tj avatar Apr 26 '11 21:04 tj

Too bad for me. :-P

If I use the same function twice (regardless of if it is imported from a different module), then the last route with that function is getting called but it is really hard to reproduce. If I swap the ‘post' and ‘get’ order, so that ‘get' is first, then the ‘post' route is called for POST and GET. Oddly enough if I rename the paths so they’re completely different, I still have the same problem. I was on node 0.5.0-pre and just tried 0.4.7 with same results.

I’ll post back when I can create a proper test case. Thanks for taking the time so far.

publickeating avatar Apr 26 '11 22:04 publickeating

The following will show the bug on my system:

var express = require('express');
var app = express.createServer();
var test1 = function(req, res) { res.send('done'); };
var test2 = function(req, res, next) { next(); };

// ending the chain with the same function causes the last route to match all previous routes with same end function
app.get('/a', function(req, res, next) { console.log('a'); next(); }, test1);
app.get('/b', function(req, res, next) { console.log('b'); next(); }, test1);
app.get('/c', function(req, res, next) { console.log('c'); next(); }, test1);

// having a unique function at the end works fine
app.get('/1', function(req, res, next) { console.log('1'); next(); }, test2, function(req, res) { res.send('done'); });
app.get('/2', function(req, res, next) { console.log('2'); next(); }, test2, function(req, res) { res.send('done'); });
app.get('/3', function(req, res, next) { console.log('3'); next(); }, test2, function(req, res) { res.send('done'); });

app.listen(3000);

Trying to get http://localhost:3000/a will print 'c' to the console, not ‘a’ as it should. If you add a unique function to the end of the chain (like /1, /2, /3) then it works (i.e. get http://localhost:3000/1 and see ‘1’ on the console).

publickeating avatar Apr 26 '11 22:04 publickeating

try with 2.3.1

tj avatar Apr 26 '11 22:04 tj

Sorry, same result still with 2.3.1. I hope that you’re able to reproduce it with the above code? I have to go, will check in later.

publickeating avatar Apr 26 '11 22:04 publickeating

ohhhh fuck.. sorry I totally misread that initially. Yeah the route's final callback (currently) has some properties stored on it for routing. I'll need to encapsulate the callback in the new Route object so that it's maintaining the state instead of the function

tj avatar Apr 26 '11 23:04 tj

This issue has been inactive for over 4 months so I'm closing it. If you think it's still an issue re-open.

  • tjbot

tj avatar Jun 17 '12 02:06 tj