BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Getting Parameter from /user/:id with req.params.id doesn't work #17

Closed nassirdreffy closed 5 years ago

nassirdreffy commented 5 years ago

ERROR MESSAGE: {"errClass":"TypeError","code":500,"message":"Cannot read property 'id' of undefined"}

const bp = require('body-parser') const anumargak = require('anumargak') const routerFactory = (options) => { return anumargak(options)}

const service = require('restana')({ server, routerFactory});

service.get('/user/:id', (req, res) => { res.send(req.params.id) })

service.start(3000)

I don't have any idea what's going on . Please help

jkyberneees commented 5 years ago

Hi nassirdreffy, please check out this example: https://github.com/jkyberneees/ana/blob/master/demos/basic-service-params.js

I will later check if this is an issue with anumargak router. For now please just try out with default router, like this:

const service = require('restana')({})

service.get('/user/:id', (req, res) => {
  res.send(req.params.id)
})

service.start()

Regards.

jkyberneees commented 5 years ago

Of course you can still override the server config if you need it:

const service = require('restana')({
  server
})
nassirdreffy commented 5 years ago

thank you very much, it works !!

so is it maybe a conflict with anumargak router . ://

I will later check if this is an issue with anumargak router.

Do you have an idea why it might be=?

Regards

jkyberneees commented 5 years ago

I will properly check and let you know. Thanks.

jkyberneees commented 5 years ago

I can reproduce:

  service.get('/name/:id', (req, res) => {
    res.send('Hello ' + req.params.id)
  })

req.params is null once using anumargak router. It suppose to be signature compatible with find-my-way router for params parsing, but it looks like not being the case. More details will follow...

jkyberneees commented 5 years ago

Hi @nassirdreffy, please consider that when using anumargak router, request params are accessible via:

req._path.params

Although I would strongly suggest to use the default find-my-way router.

Regards, Rolando