Skip to content

TypeScript code snippet – How to convert a normal app to a Angular Universal?



const express = require('express');

const fs = require('fs');

const { platformServer, renderModuleFactory } = require('@angular/platform-server');

const { ngExpressEngine } = require('@nguniversal/express-engine');

// Import the AOT compiled factory for your AppServerModule.

// This import will change with the hash of your built server bundle.

const { AppServerModuleNgFactory } = require(`./dist-server/main.bundle`);

const app = express();

const port = 8000;

const baseUrl = `http://localhost:${port}`;

// Set the engine

app.engine('html', ngExpressEngine({

 bootstrap: AppServerModuleNgFactory


app.set('view engine', 'html');

app.set('views', 'dist');

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

app.get('*', (req, res) => {

 res.render('../dist/index.html', {





app.listen(port, () => {

 console.log(`Listening at ${baseUrl}`);


See also  PHP code snippet - laravel How to check if there are record exists?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.