Coder Social home page Coder Social logo

jaychase / ngx-express-universal Goto Github PK

View Code? Open in Web Editor NEW
9.0 5.0 3.0 115 KB

angular-cli app with universal

TypeScript 65.00% JavaScript 21.85% HTML 12.07% CSS 1.07%
universal angular seo-friendly server-side-rendering pre-rendering angular-cli express

ngx-express-universal's Introduction

NgxExpressUniversal

Angular Cli (1.30) + Angular Material 2 + Angular Universal.

This is a rough guide of how to get up and running with all of the above. A blog post to accompany the code can be found here. The starting point is an Angular-cli generated app with Material 2 added. The aim of this repo is to be a guide to the minimum steps required to get everything up and running so production concerns like error handling, caching and performance etc... are not covered (but hopefully will be in a separate repo with all the best practices later).

quick links

To get started with Angular cli, to install Angular Material2, and to install Angular Universal.

getting started with the sample

git clone https://github.com/JayChase/ngx-express-universal.git
cd ngx-express-universal
npm install
npm run build-all
npm run server

steps to create the project

add universal

These steps are just a checklist for the Angular cli story so use that for full details.

  • install @angular/platform-server (--save-dev)
  • add src/tsconfig.server.json
  • add src/main.server.ts
  • add src/app.server.module.ts
  • change src/app.module.ts
BrowserModule.withServerTransition({
      appId: 'cli-uni'
    })
  • add server app to .angular-cli.json
apps: [
  ...,
   {
      "name": "uni",
      "platform": "server",
      "root": "src",
      "outDir": "dist-server",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.server.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.server.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
]
  • exclude dist-server in .gitignore

add an express server

  • install express @types/node @types/express (--save)
  • add server-src/tsconfig.json (standard node.js tsconfig with output path set to ../server)
  • add build-server script to package.json
  • add server-src/server.ts
require('zone.js/dist/zone-node');
const renderModuleFactory = require('@angular/platform-server').renderModuleFactory;
const AppServerModuleNgFactory = require('../dist-server/main.d8532c2d5f13c00de031.bundle.js').AppServerModuleNgFactory; // bundle name set by npm script build-set-server-main-bundle
// const index = require('fs').readFileSync('./dist-server/index.html', 'utf8'); // for server side rendering
const index = require('fs').readFileSync('./dist/index.html', 'utf8'); // for server to client transition
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.port || 3000;

app.use(express.static(path.join(__dirname, '../dist'), {
    index: false
}));

app.use('*', function (req, res, next) {
    // res.sendFile(path.join(__dirname,'../dist/index.html'));
    renderModuleFactory(
        AppServerModuleNgFactory, { document: index, url: req.baseUrl }).then(html => {
            res.send(html);
        });
});

app.listen(port, function () {
    console.log('listening on port: ' + port);
});
"build-server": "tsc -p ./server-src/tsconfig.json"

fix missing css bundle (for server side rendering)

To get the Material styles working on the server side add the style bundle to the server index.html.

  • install glob and @types/glob (--save-dev)
  • add build/addStyleBundle.js
  • add build-uni-add-css to package.json
"build-uni-add-css": "node build/addStyleBundle"
  • add to src/index.html
  • change server/server.ts to use dist-server/index.html
const index = require('fs').readFileSync('./dist/index.html', 'utf8'); 

update server.ts with main.*.js bundle name on build

  • add build/setServerMainBundle.js
  • add build-set-server-main-bundle
"build-set-server-main-bundle": "node build/setServerMainBundle"

chosing between server side render or server to client transition

Just switch between the server or app index.html. So for server side the index in server.ts should be:

const index = require('fs').readFileSync('./dist-server/index.html', 'utf8');

and for server client transition

const index = require('fs').readFileSync('./dist/index.html', 'utf8');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.