Reputation: 740
I'm trying to publish my website in different languages and am following the official Angular guide to localization (https://angular.io/guide/i18n).
However, when I build the project and serve it, I get the error Failed to lookup view "index" in views directory "C:\PATH_TO_PROJECT\dist\browser"
. This is because the dist/browser
directory has now been split up into the different languages. How do I configure the server to serve each language? Angular does give instructions on how to configure Nginx and Apache servers, but I couldn't find any instructions for Angular Universal.
/***************************************************************************************************
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
*/
import '@angular/localize/init';
// Polyfills required for Firebase
import 'globalthis/auto';
(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xhr2');
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import * as compression from 'compression';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import { LOCALE_ID } from '@angular/core';
const domino = require('domino');
// The Express app is exported so that it can be used by serverless Functions.
export function app(lang: string): express.Express {
const server = express();
const distFolder = join(process.cwd(), `dist/browser${lang}`);
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
const win = domino.createWindow(indexHtml);
global['window'] = win;
global['document'] = win.document;
server.use(express.json());
// 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);
// 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 }, { provide: LOCALE_ID, useValue: lang }] });
});
return server;
}
function run(): void {
const port = process.env.PORT || 4200;
// Start up the Node server
const appEn = app('en');
const appFr = app('fr');
const appDe = app('de');
const server = express();
server.use('', appEn);
server.use('/en', appEn);
server.use('/fr', appFr);
server.use('/de', appDe);
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';
I am also having trouble prerendering the app. I get the error An unhandled exception occurred: Could not find the main bundle: C:\PATH_TO_PROJECT\dist\server\en-US\main.js
Thanks in advance!
Upvotes: 2
Views: 347
Reputation: 740
I ended up using a proxy which I called server.run.js
which would route the user to the correct url.
Upvotes: 1