Webpack-dev-middleware and Webpack-hot-middleware implement express full stack hot update.

Source: Internet
Author: User
Tags node server

Scene:

Node Server.js starts the back-end server.

NPM Run Dev starts the front-end server.

When you need node's express framework to combine with Webpack, you'll use two dependencies on the topic.

Of course, if you just want to start webpack with node to implement the hot update, then webpack-dev-server, you can achieve! For details see: 1190000007374078;

The problem with this is that you can't find the API you wrote on the page, and there's a 404 error (because you didn't actually start your backend server).

Webpack-dev-middleware and Webpack-hot-middleware are needed at this time. Webpack-dev-middleware is a middleware for dealing with static resources, the Webpack-dev-server is a small Node.js Express server, It also uses Webpack-dev-middleware to process webpack compiled output; Webpack-hot-middleware is a middleware used in conjunction with Webpack-dev-middleware, It can implement the browser's non-refresh update (hot reload).

On the code!

Server.js

varText = require ('./db ')), Webpack= Require (' Webpack '), config= require ('./webpack.config ')), Webpackdevmiddleware= Require (' Webpack-dev-middleware '), Webpackhotmiddleware= Require (' Webpack-hot-middleware '); varcompiler =webpack (config);
Config.entry.unshift ("webpack-hot-middleware/client?reload=true?http://127.0.0.1:7777/"); Add this to the portal of the Webpack configuration file? reload=true Set whether the browser refreshes automatically;
App.use (webpackdevmiddleware (compiler, {noinfo:true, PublicPath:config.output.publicPath})) App.use (webpackhotmiddleware (compiler)) App.listen (7777);

Webpack.config.js

? reload=true        Sets whether the browser refreshes automatically;
    entry: [        './main.js '    ],    output: {        + '/',        '/',         ' index.js ',    },

The

Webpack-dev-middleware and Webpack-hot-middleware implement express full stack hot update.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.