Universal: Angular Universal REQUEST and RESPONSE

Created on 20 Mar 2018  路  27Comments  路  Source: angular/universal

Note: for support questions, please use one of [these channels][Channels]. This repository's issues are reserved
for feature requests and bug reports. Also, Preboot has moved [here][preboot] - please make preboot-related issues
there.

  • I'm submitting a ...
- [x ] bug report
- [ ] feature request
- [ ] support request => Please do not submit support request here, see note at the top of this template.
  • What modules are related to this Issue?
- [ ] aspnetcore-engine
- [ ] common
- [x ] express-engine
- [ ] hapi-engine
- [ ] module-map-ngfactory-loader
  • What is the current behavior?
    How to access to REQUEST and RESPONSE objects from angular components or services ??

Most helpful comment

Hello all,

I have the same error, this.response is empty :

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

@Component({
  selector: 'placeme-not-found',
  templateUrl: './not-found.component.html',
  styleUrls: ['./not-found.component.scss']
})
export class NotFoundComponent implements OnInit {

  constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    @Optional() @Inject(RESPONSE) private response: any
  ) { }

  ngOnInit() {
    if (isPlatformServer(this.platformId)) {
      this.response.sendStatus(404);
    }
  }

}
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));
"@angular-devkit/build-angular": "~0.6.3",
"@angular/animations": "^6.0.2",
"@angular/cdk": "^6.1.0",
"@angular/cli": "~6.0.3",
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/compiler-cli": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/flex-layout": "^6.0.0-beta.15",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/language-service": "^6.0.2",
"@angular/material": "^6.1.0",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/platform-server": "^6.0.2",
"@angular/router": "^6.0.2",
"@google-cloud/datastore": "^1.4.0",
"@nguniversal/common": "^6.0.0",
"@nguniversal/express-engine": "^6.0.0",
"@nguniversal/module-map-ngfactory-loader": "^6.0.0",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",

All 27 comments

This is documented in the repo:

https://github.com/angular/universal/blob/master/modules/express-engine/README.md#using-the-request-and-response

I followed that guide but the Response is undefined !

Did you import that correct tokens?

I would not use the latest release, it is broken IMO. Please try 5.0.0-beta.5

Couple of releases earlier token were here:

import { REQUEST } from '@nguniversal/express-engine/token';
then in beta.6, they were here

import { REQUEST } from '@nguniversal/express-engine';
in beta.7 they are back here

import { REQUEST } from '@nguniversal/express-engine/token';
But they are giving error in beta.7 with some injection token.

Confusing....

They indeed seem to be removed from the public API: https://github.com/angular/universal/commit/7704d56e93e5ae56693afbeb2f32fa88ed6f3c0b#diff-1f3e7b78782be8829d26d563754ad4ae

@oli4jansen do you know the alternative?

This works for me import { RESPONSE } from '@nguniversal/express-engine/tokens'; version 5.0.0-beta.7

It fails for me at runtime...

Please try @asadsahi 5.0.0-beta.5. It works for me properly. The newer versions aren't compelling enough to migrate to if these key features are broken.

5.0.0-beta.7 fixes this issue. Please upgrade to that version to restore functionality.

Was it intended to be released as an es6 package? This breaks my bundling.

At least for now, yes. The v5 final/v6 beta will again be in APF format. This beta was primarily to patch the regression quickly

Ok thanks, good to know! That is the reason I have been unable to upgrade past 5.0.0-beta.5.

@patrickmichalina What bundler do you use? Does it not support es2015?

@Toxicable I am using FuseBox. It does, but changing to es2015 breaks other stuff. I could probably work through it and configure settings just for this library, but it seems weird that this project would be the exception in the Angular project.

Hoping you can distribute either the original commonjs, umd, or even typescript source. Why not all 3? :)

@patrickmichalina Your output should remain es5 but it should be able ot injest es2015.
But you're right either way, it should work without you having to change anything.

We are planning on moving to APF down the line but we wanted to push this out as fast as possible to fix the regression with these tokens.

how can I manipulate the server Response from any angular controller?

5.0.0-beta.6 and 5.0.0-beta.7 have both been deprecated. The latest version once again points to 5.0.0-beta.5.

The new fix is in #940 and has been tested with all modules in all formats. Please feel free to pull from the remote branch and build from source to confirm.

Just like to apologise on behalf of the Universal team for this.
We've just released 5.0.0-beta.8 which should have resolved all of these issues in the testing we've done.
If you have anymore issues please feel free to open another issue.

@Toxicable thanks! This also fixed my issue upgrading to Angular 6 rc-0!!!

Hello all,

I have the same error, this.response is empty :

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

@Component({
  selector: 'placeme-not-found',
  templateUrl: './not-found.component.html',
  styleUrls: ['./not-found.component.scss']
})
export class NotFoundComponent implements OnInit {

  constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    @Optional() @Inject(RESPONSE) private response: any
  ) { }

  ngOnInit() {
    if (isPlatformServer(this.platformId)) {
      this.response.sendStatus(404);
    }
  }

}
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));
"@angular-devkit/build-angular": "~0.6.3",
"@angular/animations": "^6.0.2",
"@angular/cdk": "^6.1.0",
"@angular/cli": "~6.0.3",
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/compiler-cli": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/flex-layout": "^6.0.0-beta.15",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/language-service": "^6.0.2",
"@angular/material": "^6.1.0",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/platform-server": "^6.0.2",
"@angular/router": "^6.0.2",
"@google-cloud/datastore": "^1.4.0",
"@nguniversal/common": "^6.0.0",
"@nguniversal/express-engine": "^6.0.0",
"@nguniversal/module-map-ngfactory-loader": "^6.0.0",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",

My bad :-)

I solve it by changing :

app.get('*', (req, res) => {
  res.render('index', { req });
});

by

app.get('*', (req, res) => {
  res.render('index', { req, res });
});

@robinComa what is the change?

Sorry, bad copy paste, I update the post ;-)

@robinComa I solute you, this is the only time a saw a working example of this feature

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings