Or how I modernized Picker router, but first some background on what is going on in Meteor on the server side when it comes to routing.
I'm using React withreact-router
without SSR. Recently I decided that I wanted to add RSS for my blogs and I wanted it to be generated on the server. So I needed some way to establish a route on the server and have it execute my function.
Now, going back in history this is relatively easy with Meteor centric routers likeiron:router
orflow:router
where we could set server routes. Under the hood Meteor usesconnect so there was already an infrastructure to get server requests and is accessible viameteor/webapp
.
import{WebApp,WebAppInternals}from'meteor/webapp'
In my case my previous use was to manually set browser policy headers without any package (but that is for another article). Now you can easily get server requests and intercept static routes as we see inconnect
documentation (here adjusted for Meteor):
WebApp.rawConnectHandlers.use('/foo',functionfooMiddleware(req,res,next){// req.url starts with "/foo"next()})
Now if you want to add params into the route you are out of luck. Connect is a minimalist library and doesn't do param resolution. Now there are quiet a few routers fornode
, most famouslyexpress
. But that is an overkill since we already have the basics and integrating any of them would most likely break your Meteor app. So what are you to do if you don't want to build it yourself. Thankfully there already is a solution.
After few hours I found an old projectmeteorhacks:picker
which provides a server side router for Meteor (it is the part that handles server side routing forflow:router
) and plays nice with anything else you might have setup.
Now the problem is thatmeteorhacks:picker
is 5 years old! Probably not something you want to add to a new project. Well, worry not! I have done the work of modernizing the package and right now I'mlobbying for it to be moved toMeteor Community Packages so that we can update the package without having to create a new namespace on Atmosphere. So for now you can go tomy repository and download the code into your packages folder.
Use Picker
After that you can install picker (it will pick your local version):
meteor add storyteller:picker
Now on your server you can import Picker:
import{Picker}from'meteor/storyteller:picker'
For my RSS I want to only handle GET requests so I create a filter:
constrssRoutes=Picker.filter(function(req,res){returnreq.method==="GET"})
After that I can now create my routes to handle building the RSS:
rssRoutes.route('/blog/:userId/rss',(params,req,res)=>{if(!params.userId)returnreturn404(res)constdata=articlesData(BLOG_TYPES.USER,params.userId)if(!data)returnreturn404(res)res.setHeader('content-type','application/rss+xml')res.writeHead(200)res.end(buildRss(data))})
Simple, isn't it?params
passes in the params you have defined in the route and underparams.query
you will get an object with search parameters from routes like/blog/storyteller/rss?output=xml
.
Where to get this?
UPDATE: My version of Picker has become part of Meteor Community Packages and you can get it fromAtmosphere.
So where is this available? Currently you can download the code frommy repository to your local packages.
Not ideal, I know. I haveopened an issue on the original Picker to move it toMeteor Community Packages, but given that Arunoda has not been responsive we will most likely have to take my codebase and publish it under MCP namespace. But hopefully in few weeks I will have an update here.
If you like my work, please consider supporting me onGitHub Sponsors ❤️.
Top comments(1)
For further actions, you may consider blocking this person and/orreporting abuse