Skip to content

Enable enhanced customization of server options#292

Open
JakeDetels wants to merge 3 commits into
broccolijs:mainfrom
JakeDetels:master
Open

Enable enhanced customization of server options#292
JakeDetels wants to merge 3 commits into
broccolijs:mainfrom
JakeDetels:master

Conversation

@JakeDetels

Copy link
Copy Markdown
Contributor

This pull request aims to enhance the user's ability to customize the options passed to broccoli.server.serve() in a couple different ways.

First, users can customize the http response headers created within lib/middleware.js by including a headers property in the main options object passed in broccoli.server.serve(builder, options). For example, the following options would enable Broccoli to include a CORS header when serving files on http://localhost:4200:

var options = {
  headers: {
    'Access-Control-Allow-Origin': '*'
  }
};

broccoli.server.serve(builder, options);

To allow advanced options like these to be easier configure when using Broccoli CLI, this pull request also expands on lib/cli.js by allowing users to define an optional .broccoli JSON-formatted file within the root directory of their projects. Any configurations defined within that .broccoli file will be pulled whenever calling broccoli serve. For example, the following .broccoli file would cause Broccoli to serve on port 12345 with a CORS header:

//  `.broccoli` file saved in project root
{
  "headers": {
    "Access-Control-Allow-Origin": "*"
  },
  "port": 12345
}

This pull request includes 5 new passing tests in test/options_test.js. To help clean up the console output when running Mocha, there is new option defined called disableLogging which (when set to true) will cause lib/server.js to not call console.log() while the Mocha tests are running.

@JakeDetels

Copy link
Copy Markdown
Contributor Author

@joliss: Any feedback?

@oksuz

oksuz commented May 25, 2016

Copy link
Copy Markdown

@joliss @JakeDetels When do you merge this feature? I need this feature for adding custom headers to server response. Thank you

@stefanpenner

Copy link
Copy Markdown
Contributor

@JakeDetels / @oksuz sorry for the long delay in response. I believe in broad-strokes I agree with this approach.

The code-base has changed some, if you are still using broccoli and would like to rebase and update the code. I would love to revisit this, and help get it in.

@stefanpenner stefanpenner mentioned this pull request Nov 23, 2017
@JakeDetels

Copy link
Copy Markdown
Contributor Author

@stefanpenner: I'll aim to get this PR updated by the end of the month (12/31/2017). If needed sooner, let me know.

@rwjblue

rwjblue commented Mar 19, 2018

Copy link
Copy Markdown
Member

Friendly ping 🏂

@simonc

simonc commented Oct 27, 2019

Copy link
Copy Markdown

Faced the issue today, this PR would be very helpful 😊

@lifeart

lifeart commented Sep 26, 2021

Copy link
Copy Markdown
Contributor

I like idea about additional customization, but I think having multiple ways to do it - not an optimal direction.

We could have .broccoli.js (no need to escape comments, able to have dynamic logic in it for different purposes - serve, build-dev, build-prod) file, with all needed things, including middleware, but it's requires more design work here.

// .broccoli.js

module.exports = async function({env, mode} : { env: 'prod' | 'dev'; mode: 'build' | 'serve' }) {
   return {
      port: 4011,
      host: '192.168.1.1',
      staticHeaders: {
         'Access-Control-Alow-Origin': '*'
      },
      middleware: {
         onError(req, resp, error) {
             //tbd
         },
         onRequest(req, resp, next) {
            //tbd
         },
         postRequest(req, resp) {
             //tbd
          }
      }
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants