angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Integrating angular2 Universal with angular CLI project

Open vinodbhargava opened this issue 10 years ago • 214 comments

  1. OS - Windows 8

  2. Versions - angular-cli: 1.0.0-beta.5 node: 4.3.1 os: win32 x64

  3. Yes, the app was created using CLI

  4. Steps - A. Created the app using following command - ng new testApp

    B. Installed angular2-universal dependencies as mentioned in -
    Angular 2 Universal quickstart

    C. Deployed client using command - ng server (server started on port 4200) Started express server through - node server.js (server started on port 3000)

    D. Accessed the client and got following message in browser - test-app works! But when accessing http://localhost:3000 it gives below error message in console -

EXCEPTION: Error: Protocol "d:" not supported. Expected "http:". STACKTRACE: Error: Protocol "d:" not supported. Expected "http:". at new ClientRequest (_http_client.js:52:11) at Object.exports.request (http.js:31:10) at Object.exports.get (http.js:35:21) at D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\angular2-universal\dist\node\platform\node_xhr_impl.js:53:22 at ZoneDelegate.invoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:281:29) at Object.NgZoneImpl.inner.inner.fork.onInvoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone_impl.js:45:41) at ZoneDelegate.invoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:280:35) at Zone.run (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:174:44) at NgZoneImpl.runInner (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone_impl.js:76:71) at NgZone.run (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone.js:223:66) D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\application_ref.js:302 throw e; ^

Kindly suggest if i had missed any step.

Any help will be appeciated.

vinodbhargava avatar Jun 09 '16 08:06 vinodbhargava

same here

a5hik avatar Apr 12 '17 21:04 a5hik

Any updates?

IPRIT avatar Apr 14 '17 05:04 IPRIT

I took a look at this repo example https://github.com/evertonrobertoauler/cli-universal-demo, and it actually worked. I ran into a few issues though. If you're using BrowserAnimationsModule, it'll prevent the server from loading at all. NoopAnimationsModule works, but obviously with less material flair. Another issue is that you can't seem to run it in anything other than prod mode. Either way, it worked and you can still use ng serve for local work.

thePunderWoman avatar Apr 17 '17 04:04 thePunderWoman

Thanks @janiukjf for sharing the demo.. Finally I was able to get the ssr working in my proj without ejecting cli by following that example..

kaleemxii avatar Apr 17 '17 13:04 kaleemxii

@janiukjf I took this demo 1 week ago and it's not working properly as well. For example, AngularFire 2 is not working for SSR with this repo. My entire project tied with firebase and that's why I cannot refuse from it.

IPRIT avatar Apr 17 '17 13:04 IPRIT

@IPRIT Yeah, I'm probably going to have to eject from the cli to get animations working properly. There doesn't seem to be a way to get BrowserAnimationsModule working with this technique since it all uses the same app module.

thePunderWoman avatar Apr 17 '17 14:04 thePunderWoman

@janiukjf The problem is, you really don't want to just have a ServerModule inherit from AppModule (if you need to split Browser/Server platform dependencies).

What you want is a Browser module and a Server module (that both import app.module which only has the most common things). Then Browser-app.module has BrowserModule in it, AnimationsModule etc. Server has ServerModule, NoOpAnimationsModule etc.

+                      app.module            (all the most common things)
+                    ^                ^
+    browser-app.module           server-app.module

Hope that helps!

Check here for an example of how you could be splitting platform logic.

MarkPieszak avatar Apr 17 '17 20:04 MarkPieszak

@MarkPieszak Yeah, I assumed as much. I will definitely be working on that as soon as I find some more time. I'll definitely be looking at that example when I do. Thanks for the pointer.

thePunderWoman avatar Apr 17 '17 20:04 thePunderWoman

Could someone point out how to get the http calls working during server side rendering ? Requests fail with this error

Response:{ _body: ProgressEvent:{ type: 'error', ........ status: 0, ok: false, statusText: '', type: 3, url: null

Do I need to import some extra HTTP provider for SSR calls to work (while using the platform-server library) ?

kaleemxii avatar Apr 17 '17 20:04 kaleemxii

@kaleemxii you need to provide absolute url to angular http

hheexx avatar Apr 17 '17 22:04 hheexx

Thanks @hheexx , however my error was related to the way I was overriding http service in a shared module, after I moved that to main app module, it worked.

kaleemxii avatar Apr 18 '17 13:04 kaleemxii

Hello everybody, I have an angular 4 cli project and I need to implement SSR through the prerender logic so that I can upload it onto an amazon S3 bucket. Is there any guide or how-to that I can follow?

Thanks.

stratio84 avatar May 03 '17 10:05 stratio84

Has anyone experimented with this angular-ssr project? I found it through The Unofficial Angular Docs. Sounds like a decent solution for an Angular 4 application...

kblestarge avatar May 03 '17 15:05 kblestarge

any news

mohamed-rekiba avatar May 04 '17 19:05 mohamed-rekiba

@MuhammadShaban I got my app working with cli and SSR using several examples. This is the one that worked for me with separate browser and server builds. https://github.com/pquarme/cli-universal-demo

thePunderWoman avatar May 04 '17 20:05 thePunderWoman

@janiukjf thank you so much, i will try it

mohamed-rekiba avatar May 05 '17 19:05 mohamed-rekiba

@janiukjf is AOT server/client side included in https://github.com/pquarme/cli-universal-demo ?

vforv avatar May 06 '17 17:05 vforv

@vforv Yup. It is. The only weirdness it has is with SCSS compilation on server side, but that is also included. They use gulp to process the sass files and then run webpack to properly process them. I want to spend some time trying to get this to work without gulp, but I haven't made it a priority yet.

thePunderWoman avatar May 06 '17 17:05 thePunderWoman

@janiukjf how can I check that? I can not find tsconfig-aot.json and rollup files...

vforv avatar May 06 '17 17:05 vforv

Does pre rendering come out of the box with any of the mentioned examples? Would it be possible to render multiple static html/css assets for individual pages with these setups? Now, the only static html page rendered is the index file. I'm using evertonrobertoauler/cli-universal-demo

AntonScotte avatar May 06 '17 20:05 AntonScotte