其他分享
首页 > 其他分享> > webpack4 express中间件webpack-dev-middleware线上运行环境

webpack4 express中间件webpack-dev-middleware线上运行环境

作者:互联网

1、下载
	cnpm install webpack-dev-middleware -D
	
2、配置
	const webpack = require('webpack');
	const middleware = require('webpack-dev-middleware');
	const config = require('../webpack.config'); 配置webpack
	const compiler = webpack(config);
	const express = require('express');
	const app = express();
	
	app.use(
	  middleware(compiler, {
	    // webpack-dev-middleware options
	  })
	);
	
	app.listen(3000, () => console.log('Example app listening on port 3000!'));

3、若报错Can't resolve 入口文件的错误
	在webpack配置中添加上下文环境
	module.exports = {
	  context:path.resolve(__dirname),
	}

代码示例:

const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const config = require('../webpack.config');
const compiler = webpack(config);
const express = require('express');
const app = express();

app.use(
  middleware(compiler, {
    // webpack-dev-middleware options
  })
);

app.get('/us', (req, res) => {
  res.json({ name: 'jeff' });
})

app.listen(3000, () => console.log('Example app listening on port 3000!'));

标签:webpack4,const,app,express,中间件,require,webpack,middleware
来源: https://blog.csdn.net/weixin_43294560/article/details/113854592