简体   繁体   English

将@ angular / cli与服务器端EJS模板集成

[英]Integrate @angular/cli with server-side EJS templates

Goal: Modify Open Graph meta tags when web crawlers visit different routes. 目标:当网络爬虫访问不同的路由时,修改“开放图”元标记。

I know Angular2 4.0.0 has a MetaService, and there is always jQuery, but web crawlers except Googlebot don't execute Javascript, so it is kind of useless to my purpose. 我知道Angular2 4.0.0具有MetaService,并且总是有jQuery,但是除Googlebot之外的网络爬虫都不执行Javascript,因此对我的目的来说这是无用的。 While Angular Universal sounds like an overkill for changing a couple meta tags. 虽然Angular Universal听起来对于更改几个元标记来说是过大的选择。

So far my solution is to copy and paste the compiled index.html in /dist to index.ejs, and modify the necessary fields. 到目前为止,我的解决方案是将/ dist中的已编译index.html复制并粘贴到index.ejs,并修改必要的字段。 Is it possible to integrate the workflow with the angular-cli compilation process, by having the entry point changed from index.html to index.ejs? 通过将入口点从index.html更改为index.ejs,是否可以将工作流程与angular-cli编译过程集成在一起? If not, what are the alternatives that I should explore? 如果没有,我应该探索哪些替代方案?

In my index.ejs : 在我的index.ejs中:

<meta property="og:url" content="<%= url %>" />

In my Express route index.js : 在我的Express路线index.js中:

res.render('index', {
  url: site_url,
});

In my server.js: 在我的server.js中:

app.set('views', path.join(__dirname, '/dist'));
app.set('view engine', 'ejs');

Please refrain your answer to the current @angular/cli version (v1.0.1 compatible). 请不要回答当前的@ angular / cli版本(兼容v1.0.1)。

Some related discussions: Add customization options for HtmlWebpackPlugin #3338 Conditional template logic in index.html for beta.11-webpack #1544 Provide config to rename index.html in dist folder #2241 一些相关的讨论: 为HtmlWebpackPlugin#3338添加自定义选项, 在beta.11-webpack#1544的 index.html中提供 条件模板逻辑 提供配置以重命名dist文件夹#2241中的index.html

I was able to solve this by using Nunjucks to render templates served via Angular Universal. 我可以使用Nunjucks渲染通过Angular Universal服务的模板来解决此问题。 I'm sure it's possible to use other engines such as EJS as well. 我确信也可以使用其他引擎,例如EJS。 Here are the relevant portions of my server.ts: 这是我的server.ts的相关部分:

import * as dotenv from 'dotenv';
dotenv.config();

import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import * as nunjucks from 'nunjucks';

import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';

import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// Express server
const app = express();

const PORT = process.env.PORT || 4201;
const DIST_FOLDER = join(process.cwd(), 'dist');

// Our index.html we'll use as our template
const template = readFileSync(join(DIST_FOLDER, 'index.html')).toString();

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist-server/main.bundle');

const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');

nunjucks.configure(DIST_FOLDER, {
  autoescape: true,
  express: app,
});

app.engine('html', (_, options, callback) => {
  renderModuleFactory(AppServerModuleNgFactory, {
    // Our index.html
    document: template,
    url: options.req.url,
    // DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP)
    ]
  }).then(html => {
    callback(null, html);
  });
});

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

// Server static files from dist folder
app.get('*.*', express.static(DIST_FOLDER));

// All regular routes use the Universal engine
// You can pass in server-side values here to have Nunjucks render them
app.get('*', (req, res) => {
  res.render('index', { req, yourVar: 'some-value' });
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM