express+webpack+ejs 运行报错

发布于 2022-09-07 21:59:26 字数 3836 浏览 11 评论 0

我的目录图片描述

package.json 如下:

.....
  "scripts": {
    "server": "node ./bin/www",
    "build": "webpack",
    "start": "webpack-dev-server --open"
  },
.....

webpack.config.js如下:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: {
        app: './public/js/index.js'
    },
    devtool: 'inline-source-map',
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new UglifyJsPlugin({
            test: /\.js($|\?)/i,
            uglifyOptions: {
                warnings: false,
                sourceMap: true,
                output: {
                    beautify: false,
                    comments:false,
                }
            }
        }),
         new HtmlWebpackPlugin({
             filename: 'index.html',
             template: './views/index.ejs',
             title:'aaa',
             minify: {
                 collapseWhitespace: true,
                 removeAttributeQuotes: true,
                 removeComments: true
             }
         }),
        new webpack.HotModuleReplacementPlugin()
    ],
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist'
    }
};

app.js 如下:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();
var webpack = require('webpack');
var webpackconfig=require('./webpack.config.js');
var compiler = webpack(webpackconfig);
app.use(require('webpack-dev-middleware')(compiler, {
    publicPath: webpackconfig.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler,{
    log: false,
    heartbeat: 2000
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
app.listen(90, function () {
    console.log(`http://localhost:${90}`);
});

process.on('exit', function (code) {
    console.log(`即将退出,退出码:${code}`);
});

process.on('uncaughtException', (err) => {
    console.log(err, 'err')
});

index.ejs 如下:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/css/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
  <script src="/dist/app.bundle.js"></script>
  </body>
</html>

运行 npm run server 后 ,报错,如图:
图片描述

index.ejs里 title 报错 未定义,初次尝试express 和 webpack,在本站搜过相关问题,未能找到解决办法,所以有此一问,请问该如何解决?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文