Hi,
typescript
@Get("/test/param1/:param1/param2/:param2")
test(@Param('param1') param1: string, @Param('param2') param2: string, @Req() request: Request, @Res() response: Response)
{
return {
'params' : request.params
}
}
When i call http://localhost:3000/default/index/test/param1/99/param2/77
=> {"params":{"param1":"99","param2":"77"}}
But if i call http://localhost:3000/default/index/test/param2/77/param1/99
=> Cannot GET /default/index/test/param2/77/param1/99
Is it correct?
When submitting a form, how can we be sure of order of submitted fields ?
@booradleys it looks like you have /test/param1 hard coded as a non-parameterized portion of the URL, so it makes sense that /test/param2/... would 404. It has nothing to do with parameter order.
Yes i understant that @marshall007 .
So can you tell me what is the route that accept all params.
/param1/value1/param2/value2/param3/value3/...../paramN/valueN ?
If i do something like this:
typescript
@Get("/test/:param")
test(@Params('param') param: any, @Req() request: Request, @Res() response: Response)
{
return {
'params' : request.params
}
}
When i call http://localhost:3000/default/index/test/param1/value1/param2/value2/param3/value3
I get:
{"params1":{"0":"param1/value1/param2/value2/param3/value3"},"params2":{"0":"param1/value1/param2/value2/param3/value3"}}
Params are not extracted from string.
I may build a regular expression to extract them in each action but i think this module purpose a easy way to do that.
Do you have a solution?
David
OMG... I'm impressed that you can code in Typescript, use this library and you don't know what a query string is 馃槅 So here you are:
https://en.wikipedia.org/wiki/Query_string
You may also need to read this:
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data
Answering your question:
@Get("/test")
test(@QueryParam("param1") param1: string, @QueryParam("param2") param2: string, @Req() request: Request, @Res() response: Response)
{
console.log(`${param1} ${param2}`);
}
So when you go to the route /test?param1=hello¶m2=world, it will log "hello world" on your node.js console.
Is it correct?
Yes. So this "issue" can be closed as it's working as intended.
I think you did not answered my question @19majkel94 , maybe you did not understand because my english is very poor.
I migrate a big php5 project (ZF2+Doctrine2) with hundreds classes to typescript/Express/Angular.
I don't want to have to declare each param in these thundands actions (routes) so i'm looking for a generic route that accept any params if that's possible.
route params are for static parameters, its not really clear what you want to do. Probably you should use query params for your purposes.
it's called a "wilcard route" in Zend Framework.
php
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin[/:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
),
),
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'module' => 'Admin',
'controller' => 'Index',
'action' => 'index',
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'Wildcard',
'options' => array(
'key_value_delimiter' => '/',
'param_delimiter' => '/'
)
)
)
)
)
)
Express doesn't provide that kind of magic as passing multiple parameters should an can be easily done with query params.
All you can do is to create a wildcard route /test/* and parse the param string by yourself:
@Get("/test/*")
test(@Params() params: any[])
{
const params = params[0].split('/');
return params;
}
wilcard is something specific, if you want exactly same functionality you have to implement it by your own. But I don't recommend you to go this zendish way and do it alternatively regular express way.
That's what I'm afraid of.
Thank you anyway for your help