Join the conversation

Sign in to join this conversation, and others like it, in the communities you care about.

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

ZEIT / Now

Suggestions / ideas for implementing @now/node middleware?

Suggestions / ideas for implementing @now/node middleware?

ZEIT/Now · December 6, 2018 at 4:25pm

Suggestions / ideas for implementing @now/node middleware?

ZEIT / Now · December 6, 2018 at 4:25pm
Hey. I'm trying to figure out where to start with this.
Essentially with Express you add middlewares like body-parser and you get enhanced request & response objects in your callbacks. Not only does it take away the pain of dealing with things POST / GET parsing but provides some useful helper methods like response.json() and response.redirect().
I've so far come up with a less than perfect "enhancer" that looks a bit like:
module.exports = (req, res) => enhance(req, res)( (req, res) => { // do stuff with req.body, res.json etc... } )
But it feels a little gnarly. I think I need to re-read up on currying & HoCs to be honest.
Has anyone done anything like this yet or have a good idea on a neater API for doing this sort of thing? Guessing that might be via a builder.

December 6, 2018 at 5:01pm

This will help you a lot (see comment of @rauchg): https://github.com/zeit/micro/issues/8

Edited
  • reply
  • like

DB example from https://zeit.co/blog/micro-9-1:

const setupDatabase = require('some-database')
let database

async function setup(handler) {
  database = await setupDatabase()
  return handler
}

module.exports = setup((req, res) => {
  return database.get('user')
})

  • reply
  • like

To handle errors + cors for a specific lambda I have this for example (clean and easy):

module.exports = handleErrors(cors(handler));

Edited
  • reply
  • like

Interesting topic @Robert!

Personally, I would not do anything without a proper framework, like Koa, or micro.

Function composition is very cool and it can be very delightful to work with, that is one core concept in Zeit's micro. Wow @rova linked the issue that I was looking for. Nice!

With koa, you can have a very similar experience comparing with express. It is a solid framework with its own choices.

We have an example with it: https://github.com/zeit/now-examples/tree/master/nodejs-koa

And I have a slight more complex example here: https://github.com/paulogdm/chop-my-url/tree/master/back/middlewares

  • reply
  • like

@rova ahh thanks! I think I got the decorator function signature backwards!

  • reply
  • like

I was close with what I was trying though so that's good to know I'm on the right track

  • reply
  • like

Yep you are!

  • reply
  • like

I tend to use the send() and json() functions from micro quite often. Perfect and minimal in conjunction with lambdas.

  • reply
  • like
like-fill
2
  • reply
  • like

that's super useful, @paulogdm thanks. Starting to get my head around it all now

  • reply
  • like
Your message here...

*bold*_italic_`code````codeblock```