Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何动态呈现pug文件,而不是使用静态cli index.html?_Node.js_Angular_Express_Pug_Angular Cli - Fatal编程技术网

Node.js 如何动态呈现pug文件,而不是使用静态cli index.html?

Node.js 如何动态呈现pug文件,而不是使用静态cli index.html?,node.js,angular,express,pug,angular-cli,Node.js,Angular,Express,Pug,Angular Cli,我有一个angular 2应用程序,我需要渲染index.pug,而不是使用angular cli的静态index.html 那么,对于这样的事情,最好的做法是什么呢?看,您可以覆盖angular cli网页包配置。好吧,在谷歌搜索了很多次之后,我想出了以下解决方法: 在angular cli.json中,将中的“index”:“index.html”更改为“index”:“index.pug” 将index.html重命名为index.pug,并将其内容更改为pug内容 在index.pug

我有一个angular 2应用程序,我需要渲染index.pug,而不是使用angular cli的静态index.html


那么,对于这样的事情,最好的做法是什么呢?

看,您可以覆盖angular cli网页包配置。

好吧,在谷歌搜索了很多次之后,我想出了以下解决方法:

  • angular cli.json中,将
    中的“index”:“index.html”
    更改为
    “index”:“index.pug”
  • index.html重命名为index.pug,并将其内容更改为pug内容
  • index.pug中,您应该有两条注释,用于放置样式和脚本,如下所示:

    head
      // the next comment is important to replace with styles.
      //- styles
    body
      app-root Loading...
      // the next comment is important to replace with scripts.
      //- scripts
    
  • 在根目录中创建parse-index.js并放入以下代码:

    'use strict';
    
    const fs = require('fs');
    
    const INDENT = '    ';
    
    const INDEX = './dist/index.pug';
    
    let index = fs.readFileSync(INDEX);
    index = index.toString()
      .replace(/<(\s+)?head(\s+)?>|<(\s+)?\/(\s+)?head(\s+)?>/g, '');
    
    let linkPattern = /<(\s+)?link[^<>]+\/?(\s+)?>/g;
    
    let links = index.match(linkPattern);
    
    let scriptPattern = /<(\s+)?script[^<]+<(\s+)?\/(\s+)?script(\s+)?>/g;
    
    let scripts = index.match(scriptPattern);
    
    index = index.replace(linkPattern, '');
    index = index.replace(scriptPattern, '');
    
    scripts.forEach((script, index) => {
      scripts[index] = script.replace(/<|>.+/g, '').replace(/\s/, '(') + ')';
    });
    
    links.forEach((link, index) => {
      links[index] = link.replace(/<|\/(\s+)?>(.+)?/g, '')
        .replace(/\s/, '(') + ')';
    });
    
    index = index.replace(
      /\/\/(\s+)?-?(\s+)?scripts/g, scripts.join('\n' + INDENT)
    );
    
    index = index.replace(/\/\/(\s+)?-?(\s+)?styles/g, links.join('\n' + INDENT));
    
    fs.writeFileSync(INDEX, index);
    
    “严格使用”;
    常数fs=要求('fs');
    常量缩进=“”;
    常量索引='/dist/INDEX.pug';
    让index=fs.readFileSync(index);
    index=index.toString()
    .替换(/|/g,”);
    设linkPattern=/g;
    让links=index.match(linkPattern);
    让scriptPattern=/{
    links[index]=link.replace(/(.+)?/g',)
    。替换(/\s/,“(”)+“)”;
    });
    index=index.replace(
    /\/\/(\s+)-(\s+)脚本/g,scripts.join('\n'+缩进)
    );
    index=index.replace(/\/\/(\s+)-?(\s+)?style/g,links.join('\n'+INDENT));
    fs.writeFileSync(索引,索引);
    
  • 最后,在postinstallpackage.json中,放置以下内容:
    ng build--prod&&node parse index.js
我希望如果有人介绍一个更好的方式