How can I get separation by QueryString? #1335
-
Our API Url is I can use req.url.searchParams.get() to get nickname or username But I don't know how to separate url by QueryString Please tell how I separate url by QueryString rest.get(/^http:\/\/localhost:(3000|6006)\/members\/signup\/exists/, (req, res, ctx) => {
console.log(req.url);
if (!req.url.searchParams.get('username')) {
return ctx.fetch(req);
}
console.log('usernameRequest');
const id = req.url.searchParams.get('username');
const existedID = validMemberEmail.find(member => member.ID === id);
if (existedID) {
return res(ctx.status(200), ctx.json({ unique: false }));
}
return res(ctx.status(200), ctx.json({ unique: true }));
}),
rest.get(/^http:\/\/localhost:(3000|6006)\/members\/signup\/exists/, (req, res, ctx) => {
console.log('first');
if (!req.url.searchParams.get('nickname')) {
const nicknameResponse = ctx.fetch(req);
console.log(nicknameResponse);
}
const id = req.url.searchParams.get('nickname');
return res(ctx.status(200), ctx.json({ nickname: false }));
}), |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, @JUDONGHYEOK. Could you please clarify what do you mean by "separate url by QueryString"? Ideally, describe what you want to do, what you're doing to achieve that, and what happens in actuality. The setup above looks correct. One thing, I'd suggest you use I generally recommend writing handlers per route. That's also the reason why query parameters mustn't include query params in the handler paths. rest.get('/my/resource', (req, res, ctx) => {
if (req.url.searchParams.has('username') {
// handle scenarion 1.
return someResponse
}
if (req.url.searchParams.has('nickname') {
// handle scenario 2.
return anotherResponse
}
// Handle the scenario when this endpoint was hit
// without any expected query parameters.
return res(ctx.status(400))
}) |
Beta Was this translation helpful? Give feedback.
Hi, @JUDONGHYEOK. Could you please clarify what do you mean by "separate url by QueryString"?
Ideally, describe what you want to do, what you're doing to achieve that, and what happens in actuality.
The setup above looks correct. One thing, I'd suggest you use
return req.passthrough()
instead ofreturn ctx.fetch(req)
to bypass a request.ctx.fetch()
will perform another request, and then return its response as a mocked response. That's not really what you want in your case, as you're not modifying that returned response.I generally recommend writing handlers per route. That's also the reason why query parameters mustn't include query params in the handler paths.