Skip to content

Conversation

@aparzi
Copy link
Contributor

@aparzi aparzi commented Dec 6, 2025

Add custom middleware for to present an Angular-tailored message

Fix #32028

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

The error message thrown when the Angular app is served via a custom hostname is incorrect/misleading

Issue Number: #32028

What is the new behavior?

Add custom middleware for to present an Angular-tailored message when angular served by ngrok

Does this PR introduce a breaking change?

  • Yes
  • No

…pp by custom hostname

Add custom middleware for to present an Angular-tailored message

Fix angular#32028
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Dec 8, 2025
@@ -0,0 +1,116 @@
/**
Copy link
Collaborator

@alan-agius4 alan-agius4 Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to change the behaviour for a number of valid cases.

Example:

  • http://foo.localhost will now be marked as invalid
  • When adding allowed hosts, all sub-domains will also be marked as valid, but with this change they will be marked as invalid.
  • Accessing the app with an external IP, will now be marked as invalid.

My suggestion would be that instead of recreating the middleware, just replace the response.

const middlewareStack = server.middlewares.stack;
const idx = middlewareStack.findIndex(({handle}) =>
  handle.name.startsWith('hostValidationMiddleware'),
);

if (idx !== -1) {
  const middleware = middlewareStack[idx];
  const originalHandle = middleware.handle;
  middleware.handle = function angularHostValidationMiddleware(req, res, next) {
    originalHandle(
      req,
      {
        writeHead: res.writeHead.bind(res),
        end: () => res.end('Error message.'),
      },
      next,
    );
  };
}

<body>
<div class="box">
<h1>Blocked request. This host ("${hostname}") is not allowed.</h1>
<p>The Angular development server only responds to local hosts by default.</p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually fully true, as you can get a response via the external IP address.

Suggested change
<p>The Angular development server only responds to local hosts by default.</p>

.box{max-width:760px;margin:0 auto}
h1{font-size:1.5rem;margin-bottom:.75rem}
p{margin:.5rem 0}
.muted{color:#6b7280}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused

Suggested change
.muted{color:#6b7280}

</style>
</head>
<body>
<div class="box">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional NIT

Suggested change
<div class="box">
<main>

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

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serve Angular Application with ngrok

2 participants