简体   繁体   English

在节点js中与webpack捆绑后,如何呈现.ejs文件并查看它?

[英]how to render a .ejs file and view it after bundling with webpack in node js?

This is my webpack config: 这是我的webpack配置:

{
    entry: './app.js',
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: "bundle.js"
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /(node_modules)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['env', 'es2015', 'stage-2']
            }
          }
        }
      ]
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: './views/index.ejs',
        hash: true,
      })
    ],
    node: {
        __dirname: false
    },
    target: 'node'
});

This is my server code 这是我的服务器代码

    const express = require('express');
    const app = express();
    const path = require('path');
    const port = process.env.PORT || 3000;
    app.use(express.static(path.join(__dirname, '..', 'public')));

    app.set('views', path.join(__dirname, '..', 'views'));

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

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

    app.listen(port, ()=>{
      console.log(`Live on ${port}`);
    });

when i build it with webpack and run it gives me error. 当我使用webpack构建它并运行它给我错误。 I dont see the index.ejs working. 我看不到index.ejs工作。 I have tried a lot of things. 我已经尝试了很多东西。 Nothing is working. 什么都没用。

Is it possible to run the output file and it starts the index.ejs? 是否可以运行输出文件并启动index.ejs? for html files it works. 对于HTML文件,它的工作原理。 Help would be much appreciated! 帮助将不胜感激!

Add this line after you set your view engine, this should do the trick.. 在设置视图引擎之后添加此行,这应该可以解决问题。

app.set("view engine", "ejs");   
app.engine('.ejs', ejs);  // <-- this one

Apparently, it's a result of a bug in webpack and an issue was already opened . 显然,这是webpack中的错误导致的,并且已经打开了一个问题。

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

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