Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a better way to customize server code #24

Open
zenkimoto opened this issue May 16, 2022 · 2 comments
Open

Find a better way to customize server code #24

zenkimoto opened this issue May 16, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@zenkimoto
Copy link

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 zenkimoto added the enhancement New feature or request label May 16, 2022
@zenkimoto
Copy link
Author

zenkimoto commented May 16, 2022

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 zenkimoto self-assigned this May 23, 2022
@zenkimoto zenkimoto mentioned this issue May 23, 2022
5 tasks
@zenkimoto
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant