Params Parser
@lottojs/params-parser
is one of our built-in middlewares that enable you to parse the query and path parameters coming from http requests.
Supported Parameters
- Path Parameters
- Query Parameters
Path Parameters
The path parameters if there are on the handled route always will be available at req.params
object and you can retrieve it in 2 ways:
req.params
object.
lotto.get('/user/:id', ({ req, res}) => {
const { id } = req.params;
...
})
lotto.get('/user/:id', ({ req, res}) => {
const { id } = req.params;
...
})
req.param
helper.
lotto.get('/user/:id', ({ req, res}) => {
const userId = req.param('id');
...
})
lotto.get('/user/:id', ({ req, res}) => {
const userId = req.param('id');
...
})
Query Parameters
The query parameters if there are on the handled route always will be available at req.query
object and you can retrieve it in 2 ways:
req.query
object.
lotto.get('/user', ({ req, res}) => {
const { gclid } = req.query;
...
})
lotto.get('/user', ({ req, res}) => {
const { gclid } = req.query;
...
})
req.get
helper.
lotto.get('/user/', ({ req, res}) => {
const gclid = req.get('gclid');
...
})
lotto.get('/user/', ({ req, res}) => {
const gclid = req.get('gclid');
...
})
Standalone use
Is also possible to use it on a standalone way on other projects that aren't using Lotto
.
Whe export a function called paramsParser
and expects a parameter containing the url that is being caleld at the moments and returns a function where you can pass the req
object coming from your http server and also a next
function in order to your project can follow the next calls.
import { createServer } from 'node:http';
import { paramsParser } from '@lottojs/params-parser';
createServer(
async (req: IncomingMessage, res: ServerResponse) => {
...
paramsParser(req.url)(req, next())
...
},
)
import { createServer } from 'node:http';
import { paramsParser } from '@lottojs/params-parser';
createServer(
async (req: IncomingMessage, res: ServerResponse) => {
...
paramsParser(req.url)(req, next())
...
},
)
Github
If you are interested on how it works more deep, please take a look on the code on Github.