Creating express backend application in Workers
I have an existing backend application which I am trying to recreate on workers. In my current application I am using express, sequelize and postgress. While recreating the application should I use these frameworks or are there any suggested alternatives that work better with workers?
10 Replies
Itty-router is pretty good for express oriented backends
GitHub
GitHub - kwhitley/itty-router: A little router.
A little router. Contribute to kwhitley/itty-router development by creating an account on GitHub.
A major caveat is that instead of building up a response over multiple middlewares and routes, instead you build up a request and then whichever middleware / route returns a Response first, the chain ends
It is possible to do some global middleware before a response is returned at router level, but you're mostly limited to modifying response metadata unless you with to reconstruct the response from scratch
Sequelize probably won't work
Postegres will work if you route it through cloudflare hyperdrive
Otherwise, a new postegres connection will be made on every request (and destroyed)
And it won't scale well
Hyperdrive instead pools connections in each region, so you will have 100 concurrent connections maximum at any given time
Thanks @conqr . This is really helpful! One follow up question. So if Sequelize does not work, is there an alternate ORM or should I look to directly write SQL queries? Thanks in advance
There are probably some, but many ORMs directly connect to the database, so it probably wouldn't work
I recommend just writing SQL
Cloudflare has a built-in SQLite Database for Workers named D1
I recommend testing / developing using that and then you can swap out to Hyperdrive for production
Cloudflare workers have poor (or nonexistent) TCP support
Great. Will do exactly that!
Also you need to keep in mind that Workers use a modified JS v8 runtime, and not the node runtime, so a lot of node packages need to be pollyfilled / don't work
Cloudflare Docs
Node.js compatibility · Cloudflare Workers docs
Implemented Node.js runtime APIs and enablement instructions for your Worker project.
Ah yeah. Been encountering this!
Generally, if some code / package doesn't work in the browser, it'll need to be pollyfilled / won't work at all on workers