Marko: [Question] Why doesn't marko/express use the middleware pattern?

Created on 6 Mar 2017  路  2Comments  路  Source: marko-js/marko

I noticed that docs for express integration just have you require('marko/express'), which means it's using the side-effect pattern to modify the global express.response object.

The typical express middleware pattern would be:

const express = require('express')
const marko = require('marko/express')

const app = express()
app.use(marko)

Is there a specific reason marko/express doesn't use the middleware pattern?

Looking at marko/express.js, it doesn't seem like it needs to cause side-effects to integrate with express.

Using side-effects instead of the middleware pattern means that you can't isolate marko integration to a specific express app, route, or path. Granted, the current implementation is opt-in (you only call res.marko if you need to) but if more features are added to express integration, the inability to isolate the integration may become a problem.

resolved feature

Most helpful comment

Hey Jordan, that's a very fair question. That was done for performance reasons since monkey-patching the prototype chain is more efficient then monkey-patching each res object that gets passed through for every request which is what would be required if we were to use middleware.

With that said, there might be hope in using the Express mount event to get access to the parent express app so that the prototype chain can be monkey-patched. That requires more investigation though. If you or something else is able to make the following work then it is something we would consider:

const express = require('express')
const marko = require('marko/express')

const app = express()
app.use(marko()) // marko should be a function call that returns an `express()` app

Alternatively, it might not hurt to also support something like the following:

require('marko/express/response').patch(express);

All 2 comments

Hey Jordan, that's a very fair question. That was done for performance reasons since monkey-patching the prototype chain is more efficient then monkey-patching each res object that gets passed through for every request which is what would be required if we were to use middleware.

With that said, there might be hope in using the Express mount event to get access to the parent express app so that the prototype chain can be monkey-patched. That requires more investigation though. If you or something else is able to make the following work then it is something we would consider:

const express = require('express')
const marko = require('marko/express')

const app = express()
app.use(marko()) // marko should be a function call that returns an `express()` app

Alternatively, it might not hurt to also support something like the following:

require('marko/express/response').patch(express);

Closing as this has been supported for a while now. See #869 to discuss deprecating the autopatch approach.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gilbert picture gilbert  路  5Comments

westtrade picture westtrade  路  5Comments

basickarl picture basickarl  路  4Comments

mauricionr picture mauricionr  路  4Comments

tinovyatkin picture tinovyatkin  路  5Comments