我在Angular 8上使用了universal,一切都运行得很好,但我在使用新版本时遇到了一些麻烦。
如果不使用angular应用程序编译server.ts,就不能再使用ngExpressEngine了。你以前完全可以这么做。
我的express服务器有一个用js写的巨大的代码库,我不能用我的angular应用程序编译它,就像原理图一样。从基本设置开始,您只需将所有内容编译在一起,然后使用node ../dist/myapp/server/main.js
启动您的“通用”服务器。
因此,我希望我可以在没有server.ts的情况下构建我的服务器端angular应用程序,然后在需要时通过ngExpressEngine在我的主应用程序中使用该构建
====
为David answer编辑
require("zone.js/dist/zone-node");
const { ngExpressEngine } = require("@nguniversal/express-engine");
const express = require("express");
const app = express();
const { join } = require("path");
const { AppServerModule } = require("./dist/ttmodel/server/main");
const { existsSync } = require("fs");
const { APP_BASE_HREF } = require("@angular/common");
app.use(express.static(join(__dirname, "dist")));
const distFolder = join(process.cwd(), "dist/ttmodel/browser");
const indexHtml = existsSync(join(distFolder, "index.original.html"))
? "index.original.html"
: "index";
app.engine(
"html",
ngExpressEngine({
bootstrap: AppServerModule,
})
);
app.set("view engine", "html");
app.set("views", distFolder);
app.get(
"*.*",
express.static(distFolder, {
maxAge: "1y",
})
);
app.get("*", (req, res) => {
res.render(indexHtml, {
req,
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
});
});
app.listen(3000);
你是说类似的东西吗?我试过了,但它不起作用
Error: ENOENT: no such file or directory, open 'app.component.html'
在原始server.ts上,直接从'./src/main.server‘导入AppServerModule。
我试着使用@angular/platform-server
中的renderModule
和renderModuleFactory
,但我也不能让它工作。
发布于 2020-05-05 21:09:15
我可能错了(我还没有测试过这一点),但我认为你可以从编译后的angular服务器包中导入你自己的服务器中需要的任何东西。
如果您查看默认server.ts
的底部,您可以看到这些行。
// 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';
因此,如果你只是使用angular 9的新方法编译server.ts
,那么你最终会得到一个main.js
文件。您只需在您自己的代码中require
该main.js
包,就可以从中获取AppServerModule
并在您自己的代码中使用它
const {AppServerModule} = require('./dist/server/main');
https://stackoverflow.com/questions/61576727
复制相似问题