为什么我们需要 express.js 服务器上的代理才能获得 webpack 热重载服务器功能与反应路由相结合

Why do we need a proxy on an express.js server in order to get webpack hot reloading server functionality combined with react-routing

可选信息:我正在尝试制作 this project built with marty.js and webpack dev server allow entry points from react-router 以便它不仅适用于 \ 路径。

因此,我整天都在研究THIS stack overflow 答案,我无法理解以下代码背后的逻辑以及为什么这个答案有效。

retozi 回答:

I set up a proxy to achieve this:

You have a regular express webserver that serves the index.html on any > route, except if its an asset route. if it is an asset, the request gets proxied to the web-dev-server

your react hot entrypoints will still point directly at the webpack dev server, so hot reloading still works.

Let's assume you run webpack-dev-server on 8081 and your proxy at 8080.

我的代码现在看起来像这样,但为了让它工作,我稍后需要实现 marty-express。为了实现这一点,我必须首先理解 retonzis 的回答。

express.js 文件

要求

    'use strict';
    var express = require('express');
    var path = require('path');
    var logger = require('morgan');
    var bodyParser = require('body-parser');
    var config = require('../config.json');
    var webpack = require('webpack');
    var WebpackDevServer = require('webpack-dev-server');
    var proxy = require('proxy-middleware');
    var url = require('url');

代理

    var app = express(); //our express instance

    // -------- my proxy----------------------


    app.use('/assets', proxy(url.parse('http://localhost:8081/assets')));//but why do we need a proxy? This line really confuses me.

马蒂快递 **

我打算在理解这个示例后立即使用它,我只是希望它能将 react-routes 传递给 express。**

    //app.use(require('marty-express')({
    //    routes: require('../routes'),
    //    application: require('../application').Application
    //}));

快递东西

    app.get('/*', function(req, res) {
        res.sendFile(__dirname + '/index.html');
    });//if I got this straight this line just tells express to server my index.html file to all routes. My guess is this will be removed when I implement [marty-express][4].

2 个服务器 webpack-dev-server 和 express

        //----- my-webpack-dev-server------------------
    var webpackServer = new WebpackDevServer(webpack(require('../../webpack.config')), {
        contentBase: __dirname,
        hot: true,
        quiet: false,
        noInfo: false,
        publicPath: '/assets/',
        stats: { colors: true }
    });

    //run webpack hot reload server on port 8081
    webpackServer.listen(8081, 'localhost', function() {});

    //run express server on port 8080
    app.listen(8080);

有没有好心人给我解释一下这个概念,就像我昨天从火星上下来一样?

我不明白两件事:

  1. 为什么我们需要代理
  2. 如何将 react-routes 传递到 express 中,以便在被询问时在每条路线上为它们提供服务。 (也许 marty-express 可以帮助我们完成这项任务,但我猜我们可以在没有它的情况下手动完成)

呸...帮我摆脱这个文件稀缺地狱!!!请大方回答.. 记住,我昨天刚从火星过来

谢谢..!

你不知道,但这很棘手。所以第一个要求是你有一个可配置的资产根。如果您将来需要 CDN,这也会带来回报。假设这是在一个 envvar ASSET_URL 中,它在 运行 你的 webpack 开发服务器和你的 express 服务器都可用。

你需要通常的 webpack 开发服务器,加上 CORS header。这让您的主要 express 服务器仅指向 script/link 标签中的 webpack 开发服务器。

ASSET_URL 就像:http://localhost:8081

Webpack

var config = require('./webpack.config');

var port = '8081', hostname = 'localhost';

if (process.env.ASSETS_URL) {
    var assetUrlParts = url.parse(process.env.ASSETS_URL);
    port = assetUrlParts.port;
    hostname = assetUrlParts.hostname;
}

new WebpackDevServer(webpack(serverConfig), {
  publicPath: serverConfig.output.publicPath,
  hot: true,
  headers: { "Access-Control-Allow-Origin": "*" }
}).listen(port, 'localhost', function (err, result) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  console.log('Listening at ' + url.format({port: port, hostname: hostname, protocol: 'http:'}));
});

然后在你的 webpack 配置文件中你有大部分相同的垃圾。

var port = '8081', hostname = 'localhost';

if (process.env.ASSETS_URL) {
    var assetUrlParts = url.parse(process.env.ASSETS_URL);
    port = assetUrlParts.port;
    hostname = assetUrlParts.hostname;
}

...

  entry: [
    'webpack-dev-server/client?' + url.format({port: port, hostname: hostname, protocol: 'http:'}),
    'webpack/hot/only-dev-server',
    ...

  output: {
    path: __dirname + '/public/',
    filename: 'bundle.js',
    publicPath: process.env.ASSETS_URL || '/public/'

快递服务器

这里唯一的特殊之处在于您需要以某种方式将 process.env.ASSETS_URL 放入模板的局部变量中。

<head>
    <link rel="stylesheet" href="{{ assetsUrl }}/main.css">
</head>
<body>
    ...
    <script type="text/javascript" src="{{ assetsUrl }}/bundle.js"></script
</body>