I'm currently using express to handle client request. At one point I need to handle get request if user loads any page of my app. So, I want all the routes to be configured at some place.
For example:
"/about"
"/contact"
"/listing"
"/product"
Above there are multiple routes and for each route I need to write app.get('/about', handler)..like this. So rather then writing multiple get handlers I want to use these path dynamically.
Is there a best way which I can use to store all the route path at one place (not DB) and can read from there only. Currently I am thinking to use JSON where I will store all the path with method type, params etc.
Also please validate is this a correct approach to handle such things or any better way or any node module.
It's common practice to write different methods for each route.
This makes sense since you'll want to return something different for /about than for /contact.
To organise this in a scalable manner you should create different files for each route. You can look at this official Express.js example.
Related
I have a Rust app and I'd like to store routes in a database (it can be an in-memory mock for all I care), but I want to update routes at runtime. I'm ignoring a 404 route or other status routes and assuming anything off the root route of "/" would be fair game for the user to create. That means the user could create "/hello/world/test" and "/hello2/again/test" with no real pattern. Most examples I see asume there will be a pattern.
The idea is that the user would be able to eventually define a route and Rust module and function in the database and then when that route is hit, it would load the function. All the examples I found assume that the routes are known at compile time or follow a pattern.
If hyper is not the framework I should be using let me know.
Thanks!
We're using react router v6 in a project, and have a potential need to store some search results filter fields in the hash fragments of the url, i.e.
/search#fn=Bob&sn=Smith&expanded=12088&active=1
I'm wondering, is there any way to do this in react router, or do I have to roll some kind of utility to generate links and then rely on vanilla js or something to read the hash fragments of the current route in react router and would I have to manually compute the links to navigate to?
So right now /search is an object based Route, and what I really want to do is be able to specify parameters that can be passed to that route via hash fragments so that when I use something like the useParams hook I can see them?
I really want to make all the search criteria deep linkable but I don't really want to have a route like "/search/Bob/Smith/expanded/1208" or some craziness like that.
Or maybe the answer is to do something janky like this?
/search/fn=Bob&ln=Smith&expanded=12088&active=1
I understand that people use something like "/api" to send data. but lets say I have a page that has a route ("/home") I could do DB stuff and send a res.send() with the data from the DB. Should I be making a call to the /api when the user visits the home page? Why would I need both? I see people talking about api routes but I'm not sure when to use it. I'm assuming we don't do res.render in an /api route. It is mostly to send JSON. I never use api route I usually use the /home way. Should I be using the api way?
I'm confused on when to use it
Edit I guess a use case for having both is returning data for outside users and to also returning data for your website. Do people combine both?
It is just about naming convention. /api is where people keep APIs for single pages apps or mobile apps. If you have neither of those, you can use /home or for that matter, any named route for your purpose. It is just a coding practice and nothing else.
I'm creating a module that exports a method that can may be called several times by any code in node.js using it. The method will be called usually from views and it will output some html/css/js. Some of this html/css/js however only needs to be output once per page so I'd like to output it only the first time the module is called per request. I can accomplish doing it the first time the module is called ever but again the method of my module can be called several times across several requests for the time the server is up so I specifically want to run some specific code only once per page.
Furthermore, I want to do this while requiring the user to pass as little to my method as possible. If they pass the request object when creating the server I figure I can put a variable in there that will tell me if my method was already called or not. Ideally though I'd like to avoid even that. I'm thinking something like the following from within my module:
var http = require('http');
http.Server.on('request', function(request, response){
console.log('REQUEST EVENT FIRED!');
// output one-time css
});
However this doesn't work, I assume it's because I'm not actually pointing to the Server emitter that was/may have been created in the script that was originally called. I'm new to node.js so any ideas, clues or help is greatly appreciated. Thanks!
Setting a variable on the request is an accepted pattern. Or on the response, if you don't even want to pass the request to your function.
One more thing you can do is indeed, like you write, have the app add a middleware and have that middleware either output that thing.
I'm not sure if I completely understand your "problem" but what you are trying to achieve seems to me like building a web application using Node.js. I think you should use one of the web frameworks that are available for Node so you can avoid reinventing the wheel (writing routing, static files serving etc. yourself).
Express framework is a nice place to start. You can find tons of tutorials around the internet and it has strong community: http://expressjs.com/
I should preface this by the fact I am new to NodeJS, and more generally to all the coding your own web server business, so please bear with me.
What I'm trying to achieve basically is replicating Apache's alias mechanism. I should simply be able to configure a list of aliases and their corresponding path in a configuration file, then have Node serve the right resource depending on the request.
To achieve this I singled out two middleware to use on top of Connect: static and route. Route is mostly working as intended, but I have a problem grasping how static works. Specifically, my question right now would be : Is it possible to define multiple "statics" to use with connect and then choose which one you want to actually serve files through after receiving a request?
Thanks
You can define multiple static to use with connect.
app.use(express.static(__dirname + '/public1'));
app.use(express.static(__dirname + '/public2'));
The connect middleware will check if the file exists in the first directory and if not found it will check the next one.
But static means static you shouldn't use with req.
If you wish to serve files based on the req then you should set-up a dynamic route that can serve content based on the request.