Angular4 Server Side Rendering with Node.js
I follow the Angular4 server side tutorial here (the official stories) https://github.com/angular/angular-cli/wiki/stories-universal-rendering , however, it is based on CommonJs
, does anyone know how to host it on a NodeJs
server? The tutorial does not include any information related to "hosting" with NodeJs
, can somebody guide me how to do that? How to setup the environment in Ubuntu? How to create a server.js
file which serves the app?
I manage to build a server app, but I don't know how to host it on server, keep having errors related to module not found. Please help.
This is the tutorial server.js (is it compatible to node.js)?
// Load zone.js for the server.
require('zone.js/dist/zone-node');
// Import renderModuleFactory from @angular/platform-server.
var renderModuleFactory = require('@angular/platform-server').renderModuleFactory;
// Import the AOT compiled factory for your AppServerModule.
// This import will change with the hash of your built server bundle.
var AppServerModuleNgFactory = require('./dist-server/main.988d7a161bd984b7eb54.bundle').AppServerModuleNgFactory;
// Load the index.html file.
var index = require('fs').readFileSync('./src/index.html', 'utf8');
// Render to HTML and log it to the console.
renderModuleFactory(AppServerModuleNgFactory, {document: index, url: '/'}).then(html => console.log(html));
I am getting the error...
Error: Cannot find module 'zone.js/dist/zone-node'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
... ... ...
My directory structure is as such:
/var/www/example.com/public_html
dist/
dist-server/
node_modules/
server.js
I am aware that ' zone.js/dist/zone-node
' is not available, however, even if I change to ' node_modules/zone.js/dist/zone-node.js
', some similar error will appear too... Shouldn't be the nodeJs
able to recognize the ' zone.js/dist/zone-node
' path, I believe there is a config file for that, but where to find?
I wish somebody can provide us with a complete tutorial related to Angular4 server side rendering , I can't really find a working tutorial for weeks... please help.
链接地址: http://www.djcxy.com/p/31386.html