adhocteam / nytimes-library

A collaborative documentation site, powered by Google Docs.
https://nyt-library-demo.herokuapp.com/
Apache License 2.0
1 stars 1 forks source link

Find a better way to customize server code #24

Open zenkimoto opened 2 years ago

zenkimoto commented 2 years ago

Problem Description

Right now, there are no customization options for customizing server code other than creating middleware. Customizing layout, CSS styles, and authentication is pretty well defined.

https://github.com/adhocteam/nytimes-library/blob/main/custom/README.md

Feature

Allow customization of server code without modifying the base code so we can receive updates from the main New York Times repository. (Without is taken loosely, if hooks need to be created somehow, maybe there is no way around it)

Additional Information

Need a solution to change response output before sending the payload back to the client. Right now, there are hooks for preload (before the server code is called) and postload (which is called after the payload is sent). Postload here doesn't really help since the response has already been sent.

zenkimoto commented 2 years ago

The way I've been trying to customize server code is through creating middleware. Some ideas I was playing around with:

'use strict'

const router = require('express-promise-router')()

async function addFileType(req, res, next) {
  if (req.path === '/search') {
    req.on('end', () => {
      // Try to modify response payload here...
    })
  }

  next()
}

router.use(addFileType)

exports.preload = router

Another idea:

'use strict'

const router = require('express-promise-router')()

async function addFileType(req, res, next) {
  if (req.path === '/search') {
     const format = res.format
     res.format = function (body) {
        // Override original function here and
        // modify payload output
     }
  }

  next()
}

router.use(addFileType)

exports.preload = route

Personally, I think neither of these solutions are ideal. A better way would be some how override the actual handlePage() function inside the file /server/routes/pages.js without modifying original.

zenkimoto commented 2 years ago

We still need to figure out a better method of customizing server code without copying the entire file over to the custom folder. Please add ideas here.