[英]node and reactjs axios server request returning index.html file instead of json
I have setup my project with a react frontend being compiled by webpack and the server running on node and express. 我已经用webpack编译了一个React前端和在node和express上运行的服务器来设置我的项目。
When I deploy to production my requests to the server are returning the index.html file in the 'dist' folder rather than the json with the data. 当我部署到生产环境时,我对服务器的请求正在返回“ dist”文件夹中的index.html文件,而不是包含数据的json。
My webpack compiled output is at the location ./dist. 我的webpack编译输出位于./dist位置。
Here is the routing part of my server.js file: 这是我的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);
Here is part of my webpack config file: 这是我的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]
};
My request is as follows (the api route /api/chefs returns a json with user profiles (tested in development): 我的请求如下(api路由/ api / chefs返回带有用户配置文件的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.');
});
};
}
It seems as though the call that I am making from my client using axios is actually hitting the api url that isn't being recognised and therefore is being redirected to simply server the index.html file. 看来我使用axios从客户端发出的调用实际上正在击中无法识别的api网址,因此被重定向到仅服务器index.html文件。
Any help? 有什么帮助吗?
Cheers 干杯
perhaps this is the offending line: 也许这是违规行:
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
becuase this catches all your requests. 因为这会捕获您的所有请求。 If you change
/*
to /
does that fix it? 如果将
/*
更改为/
是否可以解决该问题?
I think the request can't make it to your router because /*
catches all requests and returns the index.html page. 我认为请求无法发送到您的路由器,因为
/*
捕获了所有请求并返回了index.html页面。
try: 尝试:
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
According to the webpack docs , you can specify the proxy setting as follows 根据webpack文档 ,您可以指定代理设置,如下所示
proxy: {
"/api": {
target: "https://other-server.example.com",
secure: false
}
}
Notice the "/api" rather than "/api/**". 注意“ / api”而不是“ / api / **”。
Also, it may be worth noting that they recommend using absolute paths via path.join(__dirname, "dist")
for the contentBase setting. 此外,可能值得注意的是,他们建议通过
path.join(__dirname, "dist")
为contentBase设置使用绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.