Angular Universal does not render index.html

I am in need of some help.

I want to render my Angular application with Angular Universal. I generated my server files with ng generate universal my-app (@angular/cli@1.6.0) which generated all the necessary server files (main.server.ts, tsconfig.server.ts,...). I made a server.ts file in my root directory (the code is the same as in the cli documentation) and a webpack.config.js file (also the same). The build processs finishes without an error, but when I try to open the page in the browser I get Error and nothing else (page). When I checked the network tab I saw network error. But if I try to open localhost:4000/index.html the index file gets served/rendered.

Note the console.log("does it get here"); in server.ts, It gets logged out when I check the console. But index is not rendered.

server.ts

// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';

import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';

// 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');

// 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');

const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');

app.engine('html', (_, options, callback) => {
  renderModuleFactory(AppServerModuleNgFactory, {
    // Our index.html
    document: template,
    url: options.req.url,
    // DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP)
    ]
  }).then(html => {
    callback(null, html);
  });
});

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

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  console.log("does it get here");
  res.render(join(DIST_FOLDER, 'browser', 'index.html'), { req });
});

// Start up the Node server
app.listen(PORT, () => {
  console.log(`Node server listening on http://localhost:${PORT}`);
});

I also downloaded the angular-universal-started, but it works there, so what am I doing wrong? Did anyone encounter the same error? I would really appreciate some help :D

UPDATE: I figured out that it was because of ngx-translate package. I think it is because the server side rendering doesn't support http requests and the translate file should be fetched from the file system and not with a http request (i think).

链接地址: http://www.djcxy.com/p/5308.html

上一篇: 如何制作带圆角的ImageView?

下一篇: Angular Universal不提供index.html