简体   繁体   English

节点和reactjs axios服务器请求返回index.html文件而不是json

[英]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.

相关问题 node.js 获取后请求返回索引。html 而不是 json - node.js fetch post request returning index.html instead of json POST 请求返回 index.html 正文而不是适当的响应 - POST request returning index.html body instead of appropriate response 节点 http-server 以 index.html 响应任何请求 - node http-server to respond with index.html to any request 仅返回index.html的节点服务器不加载bundle.js或其他js文件 - Node server returning only index.html don't load bundle.js or other js files Node.js Express服务index.html而不是静态文件 - Node.js Express serving index.html instead of static file node-http-server 模块以 index.html 响应任何请求 - node-http-server module to respond with index.html to any request 如何将 js 文件链接到 Node.js web 服务器中的 index.html? - How do I link a js file to index.html in a Node.js web server? 客户-服务器架构结构(带有节点js)-将index.html文件放在哪里? - client - server architecture structure (with node js) - where to put the index.html file? 如何将所有服务器请求重定向到 node.js 中的 index.html 文件 - how to redirect all server requests to index.html file in node.js Index.html 而不是服务器端的客户端内容 - Index.html instead of client stuff on server side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM