具有 Angular Universal 的离子电容器:调试断点不会被触发

问题描述 投票:0回答:0

所以我将

Angular Universal
添加到我的
Ionic Capacitor
项目中。我似乎无法调试我的项目。调试断点不会被触发。

这是我的

server.ts

/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-underscore-dangle */
/* eslint-disable prefer-arrow/prefer-arrow-functions */
import 'zone.js/node';

import { APP_BASE_HREF } from '@angular/common';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { existsSync } from 'fs';
import { join } from 'path';
import * as bodyParser from 'body-parser';

import { AppServerModule } from './src/main.server';

export const server = express();

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const serverFolder = process.cwd();
  const distFolder = serverFolder.endsWith('dist/app/server')
    ? join(serverFolder + '/../../../dist/app/browser')
    : join(serverFolder, 'dist/app/browser');
  // const indexHtml = existsSync(join(distFolder, 'index.html'))
  //   ? 'index.html'
  //   : 'index.original.html';
  const indexHtml = 'index.html';
  const indexFolder = join(distFolder, indexHtml);

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine)
  server.engine(
    'html',
    ngExpressEngine({
      bootstrap: AppServerModule,
    })
  );

  server.set('view engine', 'html');
  server.set('views', distFolder);

  server.use(bodyParser.json());
  server.use(bodyParser.urlencoded({ extended: true }));

  // require('./api/support');

  console.log('IT RUNS!');

  server.post('/api/test', (req, res, next) => {
    console.log('HASHDASHDSAHDAS');
    res.redirect(303, '/support');
  });

  server.get('/api/test', (req, res, next) => {
    console.log('GET SUCCESS');
    res.status(200).json({success: true});
  });

  // Example Express Rest API endpoints
  // server.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(indexFolder, {
      req,
      providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
    });
  });

  return server;
}

function run(): void {
  const port = process.env.PORT || 4000;

  // Start up the Node server
  const application = app();
  application.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';

我试图在这个文件的任何地方放置断点,但它没有被触发。但是

console.log('IT RUNS')
正在显示在
VSCode
日志中。

然后我尝试从我的客户端调用

server.get('/api/test')
端点。但它从不打印
console.log('GET SUCCESS')
.

这是调用

/api/test
端点的代码:

...
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.debug();
  }

  async debug() {
    this.http.get('/api/test').subscribe((res: any) => {
      console.log('GET SUCCESS');
      this.title = res.success;
    });
  }

我很难调试这个项目。我通过

npm run dev:ssr
运行项目,它调用
ng run app:serve-ssr
.

请有人给我一些建议。

谢谢

node.js angular ionic-framework capacitor angular-universal
© www.soinside.com 2019 - 2024. All rights reserved.