Node.js 角度通用页面加载/刷新问题
我在我的项目中添加了Angular Universal(升级到Angular 8)。这是一个比较大的项目。在解决了所有窗口、jquery和localstorage问题后,我已经成功地运行了该应用程序。但问题是,服务器运行在localhost:4000上。当我在浏览器中键入localhost:4000时,什么也没发生。但是,如果我放置localhost:4000/index.html,它将开始正常加载并返回基本localhost:4000。然后每个角度的路线都很好。但如果我尝试刷新任何页面,它不会加载任何内容 我的服务器.tsNode.js 角度通用页面加载/刷新问题,node.js,angular,express,server-side-rendering,angular-universal,Node.js,Angular,Express,Server Side Rendering,Angular Universal,我在我的项目中添加了Angular Universal(升级到Angular 8)。这是一个比较大的项目。在解决了所有窗口、jquery和localstorage问题后,我已经成功地运行了该应用程序。但问题是,服务器运行在localhost:4000上。当我在浏览器中键入localhost:4000时,什么也没发生。但是,如果我放置localhost:4000/index.html,它将开始正常加载并返回基本localhost:4000。然后每个角度的路线都很好。但如果我尝试刷新任何页面,它不会
import 'zone.js/dist/zone-node';
// ssr DOM
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join('dist/browser', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
Object.defineProperty(win.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
// mock documnet
global['document'] = win.document;
// othres mock
global['CSS'] = null;
// global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
global['Prism'] = null;
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
// The Express app is exported so that it can be used by serverless Functions.
export function app() {
const server = express();
const distFolder = join(process.cwd(), 'dist/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 }] });
});
return server;
}
function run() {
const port = process.env.PORT || 4000;
// Start up the Node server
const server = app();
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;
if (mainModule && mainModule.filename === __filename) {
run();
}
export * from './src/main.server';
我想我需要对此文件进行一些更改才能正确加载。您是否解决了此问题?或者你还没有,我还没有解决。有什么建议吗@AbolfazlRI仍然有同样的问题……同样的问题。现在找不到任何修复程序。我找到了这个,但没有帮助: