senchalabs / connect

Connect is a middleware layer for Node.js
MIT License
9.84k stars 1.09k forks source link

Middleware is used on every request if the parameter "route" is '/' #1094

Closed jeange1003 closed 7 years ago

jeange1003 commented 7 years ago

In the source code https://github.com/senchalabs/connect/blob/master/index.js line 101: // strip trailing slash if (path[path.length - 1] === '/') { path = path.slice(0, -1); } Which changes the path from '/' to ''. If I want to run the middleware just for homepage which path is '/', I have to pass '//' for parameter "router'.

dougwilson commented 7 years ago

Hi @jeange1003 I'm not sure I understand what you're saying. Can you perhaps provide the following?

  1. Version of connect you're using
  2. A full app that demonstrates the issue
  3. Instructions for how to run the app, what request to make, and what you expected to happen.

Thanks!

dougwilson commented 7 years ago

The title of the issue is

Middleware is used on every request if the parameter "route" is '/'

Which is just describing exactly how connect is supposed to work, so perhaps this is just a misunderstanding of how to use connect.

jeange1003 commented 7 years ago

Sorry, I misunderstood the connect usage. How to set the route if the middleware is just for '/', not for '/foo', '/xxx'?

dougwilson commented 7 years ago

Hi @jeange1003 you can't; that is not a feature of connect unless you use some routing middleware on top, like https://github.com/pillarjs/router :

var connect = require('connect');
var router = require('router');

var app = connect();
var router = router();

// Mount a router
app.use(router);

// respond to only / requests
router.get('/', function(req, res){
  res.end('Hello from Connect!\n');
});

app.listen(3000);
jeange1003 commented 7 years ago

Got it, thank you.