How to use @nguniversal/express-engine - 10 common examples

To help you get started, we’ve selected a few @nguniversal/express-engine examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anihalaney / rwa-trivia / server.ts View on Github external
const DIST_FOLDER = resolve(process.cwd(), './dist');
// console.log(DIST_FOLDER);


const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP
} = require(`./functions/dist/server/main`);

enableProdMode();


// Set the engine
app.engine(
  'html',
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)]
  })
);

app.set('view engine', 'html');
app.set('views', DIST_FOLDER);
app.use(compression());
// Point all routes to Universal
app.get('*', (req, res) => {
  // console.log('ssr url---->', req.url);
  res.setHeader('Cache-Control', 'public, max-age=21600, s-maxage=21600');

  if (req.url.includes('index.html')) {

    index = (!index) ? require('fs')
github angular / universal / integration / express-engine-ivy-prerender / server.ts View on Github external
export function app() {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/express-engine-ivy-prerender/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // app.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
github patrickmichalina / fusebox-angular-universal-starter / src / server / server.app.ts View on Github external
setHeaders: (res: express.Response, path: any) => {
      res.setHeader('Expires', isProd ? ms('1yr').toString() : ms('0').toString())
    }
  }

  // setup logger
  if (config.env === 'dev') {
    app.use(morgan('dev'))
  } else {
    const logger = createLogger({ name: 'Angular Universal App', type: 'http-access' })
    app.use(bunyanMiddleware({ logger, excludeHeaders: ['authorization', 'cookie'] }))
  }

  if (process.env.HEROKU) app.use(forceSsl)

  app.engine('html', ngExpressEngine({ bootstrap: AppServerModule }))
  app.set('view engine', 'html')
  app.set('views', root)
  app.use(cookieParser())
  app.use(shrinkRay())
  if (config.server.minifyIndex) {
    app.use(minifyHTML({
      override: true,
      exception_url: false,
      htmlMinifier: {
        removeComments: true,
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes: false,
        minifyJS: true
      }
    }))
github angular-material-extensions / freelancer-theme / server.ts View on Github external
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine(
  'html',
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)]
  })
);

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

// Example Express Rest API endpoints
// app.get('/api/**', (req, res) => { });
// Server static files from /browser
app.get(
  '*.*',
  express.static(join(DIST_FOLDER, 'browser'), {
    maxAge: '1y'
  })
github willmendesneto / micro-frontend-pages / first-page / server.ts View on Github external
const DIST_FOLDER = join(process.cwd(), 'dist');

// Our index.html we'll use as our template
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');

// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

app.use(responseTime());
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

/* - Example Express Rest API endpoints -
  app.get('/api/**', (req, res) => { });
*/

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
  maxAge: '1y'
github DSpace / dspace-angular / src / main.server.aot.ts View on Github external
enableProdMode();
  app.use(compression());

  app.use(morgan('common', {
    skip: (req, res) => res.statusCode < 400,
    stream: accessLogStream
  }));
} else {
  app.use(morgan('dev'));
}

app.use(cookieParser());
app.use(bodyParser.json());

app.engine('html', ngExpressEngine({
  bootstrap: ServerAppModuleNgFactory
}));

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

function cacheControl(req, res, next) {
  // instruct browser to revalidate
  res.header('Cache-Control', ENV_CONFIG.cache.control || 'max-age=60');
  next();
}

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

// TODO: either remove or update mock backend
// app.get('/data.json', serverApi);
github steveblue / angular2-rollup / server.universal.js View on Github external
Object.defineProperty(win.document.body.style, 'transform', {
    value: () => {
        return {
            enumerable: true,
            configurable: true
        };
    },
});

enableProdMode();

// config server to use compression, serve assets gzipped
app.use(compression());

// bootstrap app with ngUniversal
app.engine('html', ngUniversal.ngExpressEngine({
    bootstrap: ServerAppModuleNgFactory
}));

// set view engine
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'dist', 'frontend'));


// handle requests
app.get('*.*', express.static(path.join(__dirname, 'dist', 'frontend')));

app.get('*', (req, res) => {
    global['navigator'] = req['headers']['user-agent'];
    res.render('index', { req, res });
});
github jamesdaniels / onSnapshot / server.ts View on Github external
// Faster renders in prod mode
enableProdMode();

// Express server
export const app = express();

const DIST_FOLDER = join(process.cwd(), 'dist');
const APP_NAME = 'onsnapshot';

const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require(`./dist/${APP_NAME}-server/main`);

// index.html template
const template = readFileSync(join(DIST_FOLDER, APP_NAME, 'index.html')).toString();

app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, APP_NAME));

// Serve static files 
app.get('*.*', express.static(join(DIST_FOLDER, APP_NAME)));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  res.render(join(DIST_FOLDER, APP_NAME, 'index.html'), { req });
});
github patrickmichalina / fusing-angular / .dist / server.js View on Github external
// app.use(cors())
expressApp.use(minifyHTML({
    override: true,
    exception_url: false,
    htmlMinifier: {
        removeComments: true,
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes: false,
        minifyJS: true
    }
}));
expressApp.use('/favicon.ico', function (req, res) { return res.sendStatus(204); }); // TODO: FAVICONS
expressApp.use('/js', express.static(publicDir + "/js"));
expressApp.set('view engine', 'html');
expressApp.engine('html', express_engine_1.ngExpressEngine({ bootstrap: server_angular_module_1.AppServerModule }));
var virtualIndex = function (req, res, doc, path) {
    if (path === void 0) { path = publicDir + "/index.html"; }
    var resolvedPath = path_1.resolve(path);
    promises_1.stat(resolvedPath)
        .then(function () {
        res.render(resolvedPath, {
            req: req,
            res: res,
            document: doc
        });
    })
        .catch(function () { return promises_1.writeFile(resolvedPath, '')
        .then(function () { return virtualIndex(req, res, doc, path); }); });
};
expressApp.get('**', function (req, res) {
    var document = "\n\n  \n    \n    <title>Fusing Angular</title>\n    \n    \n  \n  \n    \n    \n    \n  \n";
github joejordanbrown / angular-cli-universal / src / express.server.js View on Github external
var hash;
fs.readdirSync(__dirname).forEach(file => {
  if (file.startsWith('main')) {
    hash = file.split('.')[1];
  }
});


const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./main.' + hash + '.bundle');

const app = express();
const cache = apiCache.middleware;
const port = Number(process.env.PORT || 8080);
const baseUrl = `http://localhost:${port}`;

app.engine('html', ngExpressEngine({
  baseUrl: baseUrl,
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));



app.set('view engine', 'html');
app.set('views', path.join(__dirname, '/../browser'));

app.use(cache('60 minutes'));
app.use(compression());
app.use('/', express.static(path.join(__dirname, '/../browser'), {index: false}));

@nguniversal/express-engine

Express Engine for running Server Angular Apps

MIT
Latest version published 1 year ago

Package Health Score

64 / 100
Full package analysis

Popular @nguniversal/express-engine functions