2017-12-04 2 views
0

私は、webpackでコンパイルされた反応フロントエンドとnodeとexpressで実行されているサーバーをセットアップしました。ノードとreactjs axiosサーバーリクエストがjsonの代わりにindex.htmlファイルを返す

プロダクションにデプロイすると、サーバーへのリクエストによって、データとともにjsonではなくdist.hフォルダにindex.htmlファイルが返されます。

私のwebpackコンパイル出力は./distにあります。ここで

は私のserver.jsファイルのルーティング一部である:ここでは

if (process.env.NODE_ENV === 'production') { 
    app.use(express.static('dist')); 
    const path = require('path'); 
    app.get('/', (req, res) => { 
    res.sendFile(path.resolve(__dirname, 'dist', 'index.html')); 
    }); 
} 
// Use our router configuration when we call/
app.use('/', router); 

は私のWebPACKの設定ファイルの一部です:

var HtmlWebpackPlugin = require('html-webpack-plugin'); 
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({ 
    template: __dirname + '/client/index.html', 
    filename: 'index.html', 
    inject: 'body' 
}); 

module.exports = { 
    entry: [ 
    './client/index.js' 
    ], 
    output: { 
    path: __dirname + '/dist', 
    filename: 'index_bundle.js' 
    }, 
    devServer: { 
    inline: true, 
    contentBase: './dist', 
    port: 8080, 
    proxy: { '/api/**': { target: 'http://localhost:3000', secure: false } } 
    }, 
    module: { 
    loaders: [ 
     {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: {presets: ['es2015','react'], plugins: ['transform-es2015-destructuring', 'transform-object-rest-spread']}}, 
     {test: /\.jpe?g$|\.gif$|\.svg$|\.png$/i, loader: 'file-loader?name=/images/[name].[ext]'}, 
     {test: /\.css$/, loaders: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader','resolve-url-loader']}, 
     {test: /\.scss$/, loaders: ['style-loader', 'css-loader','postcss-loader', 'sass-loader','resolve-url-loader']} 
     ] 
    }, 
    plugins: [HTMLWebpackPluginConfig] 
}; 

次のように私の要求は(APIルート/ APIです/シェフはユーザプロファイルを持つjsonを返します(開発中のテスト済み):

export function listChefs() { 
    return function (dispatch) { 
    axios.get('/api/chefs') 
     .then((response) => { 
     dispatch({ 
      type: LIST_CHEFS, 
      payload: response.data 
     }); 
     }) 
     .catch((err) => { 
     errorHandler(dispatch, 'There was a problem. Please refresh and try again.'); 
     }); 
    }; 
} 

Axiosを使用しているクライアントから、実際に認識されていないapi URLにヒットしているため、単にindex.htmlファイルをサーバーにリダイレクトしています。

助けが必要ですか?

乾杯

+0

axiosリクエストはどのように見えますか? –

+0

私はaxiosリクエストで質問を更新しました –

+0

このルートを処理するために、特急でエンドポイントを定義しましたか? –

答えて

0

おそらくこれは、問題のある行である:これは、すべての要求をキャッチbecuase

app.get('/*', (req, res) => { 
    res.sendFile(path.resolve(__dirname, 'dist', 'index.html')); 
}); 

/*/に変更した場合は修正されますか?

/*がすべてのリクエストを受け取り、index.htmlページを返すため、リクエストがルータに送信されないと思います。

試してみてください。

proxy: { 
    "/api": { 
    target: "https://other-server.example.com", 
    secure: false 
    } 
} 

お知らせむしろ "/ API/**" よりも "/ API" を次のように

app.get('/', (req, res) => { 
    res.sendFile(path.resolve(__dirname, 'dist', 'index.html')); 
}); 
+0

ここで元のコードスニペットを使用していましたが、違いはありません。正しい構文を使用するように質問のコードを更新します。 –

0

webpack docsによると、プロキシ設定を指定することができます。

また、contentBase設定にはpath.join(__dirname, "dist")で絶対パスを使用することをお勧めします。

関連する問題