内容简介:在我们开发的过程中,我们会使用首先就是配置webpack的配置。新建一个我们这里需要关注的是,每个
在我们开发的过程中,我们会使用 webpack-dev-server
实现自动刷新, webpack-dev-server
会把编译后的文件全部保存在内存里,而不会写入到文件目录内。但当我们的开发是前端和后端在一个项目里的时候就不行了。我们可以使用webpack结合express实现自动编译刷新
- 配置webpack
首先就是配置webpack的配置。新建一个 webpack.config.js
文件
const path = require('path') const webpack = require('webpack') var hotMiddlewareScript = 'webpack-hot-middleware/client' module.exports = { entry: { main: ['./src/main.js', hotMiddlewareScript] }, mode: 'development', output: { path: path.resolve(__dirname, 'public'), filename: 'js/[name].js', publicPath: '/' }, resolve: { extensions: ['.js'] }, plugins: [ new webpack.optimize.OccurrenceOrderPlugin(), new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin() ] } 复制代码
我们这里需要关注的是,每个 entry
后都要增加一个 hotMiddlewareScript
,还有就是增加3个插件
首先新建一个 webpack.middleware
文件,这里我们需要用到 webpack-dev-middleware
和 webpack-hot-middleware
两个中间件。
const webpack = require('webpack') const webpackDevMiddleware = require('webpack-dev-middleware') const webpackHotMiddleware = require('webpack-hot-middleware') const webpackConfig = require('./webpack.config') module.exports = app => { let compiler = webpack(webpackConfig) app.use(webpackDevMiddleware(compiler, { publicPath: '/', stats: { colors: true, chunks: false } })) app.use(webpackHotMiddleware(compiler)) } 复制代码
注意: webpack-dev-middleware
和 webpack-hot-middleware
的静态资源服务仅仅用于开发环境。到了生产环境,应该使用 express.static()
。
- 下面编写express代码
const express = require('express') const bodyParser = require('body-parser') const {resolve} = require('path') const webpackMiddleware=require('./webpack.middleware') const router = express.Router() const app=express() const port = process.env.PORT || 3000 webpackMiddleware(app) app.use(express.static(resolve(__dirname,'./public'))) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true })) // ...代码 app.use(router) app.listen(port, () => { console.log(`Server listening on http://localhost:${port}, Ctrl+C to stop`) }) 复制代码
这样,我们在开发的时候,前端和后端都在同一个服务里运行了
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Swift iOS : 上拉刷新或者下拉刷新
- Swift iOS : 上拉刷新或者下拉刷新
- 优雅地刷新RecyclerView
- 上拉加载 下拉刷新
- jQuery ajax局部刷新
- vue项目刷新当前页
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Elements of Programming
Alexander A. Stepanov、Paul McJones / Addison-Wesley Professional / 2009-6-19 / USD 39.99
Elements of Programming provides a different understanding of programming than is presented elsewhere. Its major premise is that practical programming, like other areas of science and engineering, mus......一起来看看 《Elements of Programming》 这本书的介绍吧!