在 Angular 8 SSR 应用程序中,存在内存泄漏

发布于 2025-01-11 06:07:42 字数 1909 浏览 0 评论 0原文

我有一个有角度的 SSR 应用程序,它会导致服务器上的内存泄漏

,当许多用户使用并打开该应用程序时,它会不断增加服务器上的内存。在 ngOnDestroy 上取消订阅不起作用,内存泄漏仍然存在

无法找到什么问题以及解决问题的正确方法是什么

我附加了 server.ts 代码

import "zone.js/dist/zone-node";

import * as express from "express";

import { join } from "path";

const domino = require("domino");
const fs = require("fs");
const path = require("path");

const app = express();

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

const template = fs
  .readFileSync(path.join(DIST_FOLDER, "index.html"))
  .toString();
const win = domino.createWindow(template);

(global as any)["window"] = win;
(global as any)["KeyboardEvent"] = win.KeyboardEvent;
(global as any)["HTMLInputElement"] = win.HTMLInputElement;
(global as any)["MouseEvent"] = win.MouseEvent;
(global as any)["Event"] = win.Event;
(global as any)["document"] = win.document;
(global as any)["navigator"] = win.navigator;
(global as any)["FormData"] = win.FormData;
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap,
} = require("./dist/server/main");

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

app.set("view engine", "html");
app.set("views", DIST_FOLDER);

app.get(
  "*.*",
  express.static(DIST_FOLDER, {
    maxAge: "1y",
  })
);

app.get(/.*aspx$/, function (req, res) {
  res.redirect(301, "WEBSITE URL");
});

// All regular routes use the Universal engine
app.get("*", (req, res, next) => {
  // special for robots.txt
  if (req.url === "/robots.txt") {
    next();
    return;
  }

  res.render("index", { req });
});

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

I have an angular SSR app that causes memory leaks on the server

it keeps on increasing memory on the server when the application is used and opened by many users.Unsubscribing subscriptions on ngOnDestroy doesn't work, still memory leaks persist

Not able to find whats the issue and whats the correct way to solve the problem

I am attaching the server.ts code

import "zone.js/dist/zone-node";

import * as express from "express";

import { join } from "path";

const domino = require("domino");
const fs = require("fs");
const path = require("path");

const app = express();

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

const template = fs
  .readFileSync(path.join(DIST_FOLDER, "index.html"))
  .toString();
const win = domino.createWindow(template);

(global as any)["window"] = win;
(global as any)["KeyboardEvent"] = win.KeyboardEvent;
(global as any)["HTMLInputElement"] = win.HTMLInputElement;
(global as any)["MouseEvent"] = win.MouseEvent;
(global as any)["Event"] = win.Event;
(global as any)["document"] = win.document;
(global as any)["navigator"] = win.navigator;
(global as any)["FormData"] = win.FormData;
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap,
} = require("./dist/server/main");

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

app.set("view engine", "html");
app.set("views", DIST_FOLDER);

app.get(
  "*.*",
  express.static(DIST_FOLDER, {
    maxAge: "1y",
  })
);

app.get(/.*aspx$/, function (req, res) {
  res.redirect(301, "WEBSITE URL");
});

// All regular routes use the Universal engine
app.get("*", (req, res, next) => {
  // special for robots.txt
  if (req.url === "/robots.txt") {
    next();
    return;
  }

  res.render("index", { req });
});

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文