I've the following routes defined, I don't understand why express is trying to use the first route and throwing cast error when I call the second route. sub is not a route param, but still it is consider so.
app.get('/api/main/:id', requestId, abcController.findOne);
app.get('/api/main/sub', requestId, abcController.findAll);
Related
What structure should be used to generate a route that accepts a param(s).
Eg.
i have pages/verify.vue which produces a verify route.
However when i use
router.push({ name: 'verify', params: { phone: '+18383' } })
the route is changed to the verify page but the params are discarded because they were not preset on the route.The warning below is thrown:
[Vue Router warn]: Discarded invalid param(s) "phone" when navigating.
What to do to solve this issue without without switching to manual route definitions?
NOTE: I prefer not to have the param values in the url!
Way forward:
It seems file system routes have no way of allowing props through except through the URL. I will be moving on with Pinia (State Management) as a workaround for this task.
OP will be using Pinia for the given use case, as a workaround.
More alternatives are available here: vite-plugin-pages - Route with params/props (Not in URL)
How can I display my data from mongoDB that have collections?
When I want to display they apart, its word but not when I try to reach them all together.
For example:
My index.js:
app.use('/menu', menuController);
My menuController.js:
router.use('/morning', morningController);
router.use('/starters', startersController);
router.use('/sandwiches', sandwichesController);
router.use('/toasts', toastsController);
router.use('/kidsMeal', kidsMealController);
router.use('/salats', salatsController);
router.use('/italianCuisine', italianCuisineController);
router.use('/mains', mainsController);
router.use('/stirFried', stirFriedController);
router.use('/desserts', dessertsController);
router.use('/softDrinks', softDrinksController);
router.use('/refreshing', refreshingController);
router.use('/hotBeverages', hotBeveragesController);
router.use('/warming', warmingController);
router.use('/various', variousController);
when I reach " http://localhost:2121/menu/morning"
I'm seeing my data in JSON from my Database (even when in get '/variousController').
But when I trying to get ' http://localhost:2121/menu', I'm getting an arrow ' Cannot GET /menu'.
What I'm doing wrong?
< But when I trying to get ' http://localhost:2121/menu', I'm getting an arrow ' Cannot GET /menu'. What I'm doing wrong?
You don't have any router in your router for "/" so the URL /menu all by itself has no route handler.
If you want /menu to have a handler, then define one:
router.get('/', someHandler); // to serve /menu
FYI, is there a reason you're using router.use() instead of an http-verb-specific variety such as router.get() for your endpoint handlers? router.use() matches all verbs including GET, POST, PUT, OPTIONS, etc... which is generally not what you want for an endpoint handler. An endpoint handler should only match the verb it is designed for.
Here is my question about using Svelte/Sapper
I have a file in /src/routes/login.js
According to Sapper doc, it will create an API endpoint in http://<my_domain>/login, which it does
Now in login.js, I would like to call another API server, assume that it is http://another_server/auth
No matter how I use the fetch function: fetch("http://another_server/auth"), this.fetch("http://another_server/auth"), Svelte responses: (this.)fetch is not defined.
The documentation said that it should be run within preload(), so I wrap the fetch under export async function preload(){}, it did not work
So my question is: other than using axios, can I keep on using fetch in server side API request?
Just tested in the template [slug].json.js file, when I add the (this.)fetch function, it did not compile, same error: (this.)fetch is not defined
Thank you very much.
Install the node-fetch npm package and use that in your server routes instead of the default fetch.
(in /src/routes/login.js):
import fetch from 'node-fetch'
...
// inside your route handler
const response = await fetch('http://another_server/auth') // no error
If you are unsure if the code will be executed on the client-side or the server-side (i.e. you're looking for a universal/isomorphic approach) you can use a conditional require:
// inside your route handler
const fetch = process.browser ? window.fetch : require('node-fetch').default
const response = await fetch('http://another_server/auth') // no error
But this is probably overkill in your use case since server routes are by definition executed on the server side.
According to the document, when passing parameter through get method on express, we can easily use:
localhost:8080/example/:first_parameter
then get the value by:
let first_parameter = req.params.first_parameter;
However when the first parameter is a link, something like:
localhost:8080/example/"http://something.net"
it wil be error. So How to pass a link as parameter via route expressjs
According to the documentation https://hapijs.com/api/16.0.1#route-configuration a route method may not be of the type 'HEAD' but I do not understand why I can not override the behavior.
HEAD routes are automatically created with every GET route you define. Therefore there's very little reason you'd need to define your own.
If you want to optimize your handling of HEAD requests, simply check your GET handler for the method, and if it is head, return an empty response with the correct headers. This is only worth doing for some very expensive GET requests, where you are expecting clients to use HEAD directly.
The main reason not to support it, is that I am expecting very few developers to use this, but it will add an extra lookup for every HEAD request.
This has been already been addressed on Github.
As to further elaborate on #Ankh's response, you can check the request method property to abbreviate the response on the GET handler:
const getHandler = (req, h) => {
// HTTP status 204 -> NO CONTENT
if (req.method as string === 'head') return h.response().code(204)
// continue the GET handler logic here
}