Express always uses the last route
I like to declare my routes like this:
GET = 'get'
POST = 'post'
PUT = 'put'
PATCH = 'patch'
DELETE = 'delete'
route = (method, route, handler) -> { method, route, handler }
routes = [
route GET, '/foo/', (req, res, app) -> res.send('foo')
route GET, '/bar/', (req, res, app) -> res.send('bar')
]
When I use this as follows, Express serves "bar" no matter if I'm going to
/foo/ or /bar/.
app = express()
for { method, route, handler } in routes
app[method] route, (req, res) -> handler(req, res, app)
However, when I add the routes using app.get ... syntax, it works as
expected:
app.get '/foo/', (req, res) -> res.send('foo')
app.get '/bar/', (req, res) -> res.send('bar')
Why does the former method fail while the latter works as expected? I'm
using Express 3.3.5 and CoffeeScript 1.6.3.
No comments:
Post a Comment