question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Inject(REQUEST) not available at angular side

See original GitHub issue

I’m submitting a…

[ ] Regression [ ] Bug report [x] Feature request [x] Documentation issue or request [ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

SSR does not provide the current host for DI.

Expected behavior

“request” should not be null in angular services.

constructor(private http: HttpClient,
              @Optional() @Inject(REQUEST) private request: any,
              @Inject(PLATFORM_ID) private platformId: Object) {
    if (isPlatformServer(this.platformId)) {
      console.log(this.request.get('host')); // host on the server
    } else {
      console.log(document.location.hostname); // host on the browser
    }
  }

What is the motivation / use case for changing the behavior?

Angular needs absolute urls at server side for api calls to the backend. See: https://angular.io/guide/universal#using-absolute-urls-for-server-requests

express-engine supports this as shown under “Request based Bootstrap”: https://github.com/angular/universal/blob/master/modules/express-engine/README.md

Environment


Nest version: 5.4.0
 
For Tooling issues:
- Node version: 8.9.4
- Platform:  Windows

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ostrowskikrzysztofcommented, Sep 23, 2020

It seems that this is still problem (v.4.0.1), REQUEST is null in ng component on ssr.

My app.component.ts

import { Component, Inject, Optional, PLATFORM_ID } from '@angular/core';
import { Request } from 'express';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { isPlatformServer } from '@angular/common';

@Component({
  selector: 'workspace-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.styl'],
})
export class AppComponent {
  title = 'title';

  constructor(
    @Optional() @Inject(REQUEST) private request: Request,
    @Inject(PLATFORM_ID) private readonly platformId: any,
  ) {
    if (isPlatformServer(this.platformId)) {
      console.log('my request', this.request);
    }
  }
}

image

Providers for REQUEST and RESPONSE were removed in that commit https://github.com/nestjs/ng-universal/commit/b43e23fbc9031405937603a2f0ef4d9a82cd6d5d#diff-a721ebf1ae45ad7227e059567df31f08L39

0reactions
Mephiztophelescommented, Feb 5, 2022

my app is throwing expections on every reload, because my server doesnt get the cookies for Authentication

Read more comments on GitHub >

github_iconTop Results From Across the Web

nguniversal express-engine request nullinjector - Stack Overflow
You can't access REQUEST in browser, it is server side object passed from expressjs to ng-universal . So in the worst case your...
Read more >
Hierarchical injectors - Angular
With hierarchical dependency injection, you can isolate sections of the application and give them their own private dependencies not shared with the rest...
Read more >
Server-side rendering (SSR) with Angular Universal
The following Node.js Express code routes all remaining requests to /dist , and returns a 404 - NOT FOUND error if the file...
Read more >
Dependency injection in action - Angular
When a class requires a dependency, that dependency is added to the constructor as a parameter. When Angular needs to instantiate the class,...
Read more >
Communicating with backend services using HTTP - Angular
Before working with the HttpClientModule , you should have a basic ... 0 requests made - .subscribe() not called. req.subscribe(); // 1 request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found