Universal не выполняет HTTP-вызовы - PullRequest
3 голосов
/ 18 июня 2019

Я перемещаю приложение в Universal, чтобы получить хороший SEO на странице index.html.Главное, что мне нужно, это заголовок и несколько метасов (ключевые слова, описание, теги og и т. Д.).

Дело в том, что, если я шучу над вызовом http, который возвращает метатеги и описание, он работает иЯ могу увидеть результат в приложении «Просмотр исходной страницы».Когда я заменяю макет на реальный HTTP-вызов, который возвращает то же самое, я не получаю данные, которые отображаются, и не вижу заголовка и тегов в источнике index.html.

Сервис сервиса макетов:

  getSeo() {
    return new Promise<ISeo>((resolve, reject) => {
      setTimeout(() => {
        resolve({
          title: 'Bnei David Univercity Connect',
          metas: [
            {
              name: 'description',
              content: 'Bnei David alumni relationship will allow the alumni and students to participate in events, find jobs, stay in touch with their alumni friends etc'
            },
            {
              property: 'og:image',
              content: 'https://www.gouconnect.com/wp-content/themes/uConnect_V4/img/integration/graduway.png'
            },
            {
              property: 'og:description',
              content: 'Bnei David alumni relationship will allow the alumni and students to participate in events, find jobs, stay in touch with their alumni friends etc'
            },
            {
              property: 'og:image:title',
              content: 'Bnei David Connect'
            },
            {
              name: 'keywords',
              content: 'Alumni relationship, Israel, Bnei-david, Univercity'
            },
            {
              name: 'application-name',
              content: 'Bnei David COnnect'
            },
            {
              property: 'og:title',
              content: 'Bnei David Alumni Relationship Management'
            },
            {
              property: 'og:url',
              content: 'http://app.bneidavidconnect.com'
            },
            {
              property: 'og:image:type',
              content: 'image/png'
            }
          ]
        })
      }, 700);
    })
  }

Я добавил задержку в 700 мс, чтобы эмулировать настоящий http-вызов.

Настоящий httpcall:

  getSeo(): Promise<ISeo> {
    return this.gwApiService.get('/seo').toPromise();
  }

, который возвращает тот же вывод.Я использую iisNode.Код моего файла server.js:

const { ngExpressEngine } = require('@nguniversal/express-engine');
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const express = require('express');
const { enableProdMode } = require('@angular/core');

 const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/gw-web-app/main.js');
 require('zone.js/dist/zone-node');

const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;

enableProdMode();

const app = express();
const distFolder = __dirname + '/dist';
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [provideModuleMap(LAZY_MODULE_MAP)]    // Make Universal lazy loading enabled on server side
}));

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

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

app.get('*', (req, res) => {
  res.render('index', { req });
});

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

Файл web.config:

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>
        <rule name="universal">
          <match url=".*" />
          <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>

Пожалуйста, помогите ..

...