Webpack-dev-server代理服务器未连接

时间:2017-10-31 11:53:20

标签: javascript reactjs webpack webpack-dev-server

在我的webpack dev服务器中,我设置了一个代理,因此调用以' api /'开头的路由。可以打到后端服务器而不是webpack服务器。这是我的配置:

devServer: {
    port: 8000,
    contentBase: 'dist',
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true
      }
    }
  }

在我的快速服务器中,每当遇到样本api路由时,我都会记录一条简单的消息。

const express = require('express')
const app = express()

app.get('api/hello', function (req, res) {
  console.log('hitting API request');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
});

然而,似乎' / api'请求没有到达快速服务器,因为当本地主机:8000 / api / hello'时,没有消息被记录到控制台。到达了。这是我的webpack.config.js文件的其余部分,如果它有任何帮助:

const Path = require('path');
const Webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// plugins
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './client/index.html',
  filename: 'index.html',
  inject: 'body'
});

const CommonsChunkPluginConfig = new Webpack.optimize.CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js'});

module.exports = {
  entry: {
    app: './client/index.js',
    vendor: ['react', 'react-dom']
  },
  output: {
      path: Path.resolve('dist'),
      filename: 'index_bundle.js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader:'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader:'babel-loader', exclude: /node_modules/ }
    ]
  },
  plugins: [
    HtmlWebpackPluginConfig,
    CommonsChunkPluginConfig
  ],
  devServer: {
    port: 8000,
    contentBase: 'dist',
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true
      }
    }
  }
}

0 个答案:

没有答案