problem description
`
in the process of server rendering, cross-domain is always unsuccessful, and the address of each request for data will go to
`
the environmental background of the problems and what methods you have tried
looked for du Niang and related information, but could not find
related codes
-package.json
`
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "npm-run-all --parallel dev:**",
"dev:start": "nodemon --watch build --exec node \"./build/bundle.js\"",
"dev:build:server": "webpack --config webpack.server.js --watch",
"dev:build:client": "webpack --config webpack.client.js --watch"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.18.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"express": "^4.16.3",
"npm-run-all": "^4.1.3",
"react": "^16.4.1",
"react-dom": "^16.6.0",
"react-redux": "^5.1.0",
"react-router-dom": "^4.3.1",
"react-thunk": "^1.0.0",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"webpack": "^4.16.0",
"webpack-cli": "^3.0.8",
"webpack-merge": "^4.1.4",
"webpack-node-externals": "^1.7.2",
"webpack-dev-middleware": "^3.1.0",
"webpack-dev-server": "^3.1.1",
"webpack-hot-middleware": "^2.21.2"
}
}
`
-webpack.base.js
`
const path = require("path");
const webpack = require("webpack");
const merge = require("webpack-merge");
module.exports = {
module: {
rules: [{
test: /\.js?$/,
loader: "babel-loader",
exclude: /node_modules/,//node_modules
options: {
presets: [
"react", "stage-0",
["env", {
targets: {
browsers: ["last 2 versions"]
}
}]
]
}
}]
},
devServer: {
contentBase: path.join(__dirname, "./public"),
publicPath:"/",
host: "127.0.0.1",
port: "3000",
overlay: true, //
// open: true, //
// stats: "errors-only", //stats: "errors-only":
hot: true, //
//
proxy: {
"/api/*":{
target: "https://api.douban.com",
secure: true,
changeOrigin: true
}
}
},
plugins: [
//
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
"process.env.BASE_URL": "\"" + process.env.BASE_URL + "\""
})
],
devtool: "source-map", //
}
`
-- webpack.client.js
`
const path = require("path");
const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const clientConfig = {
mode: "development",
entry: "./src/client/index.js",
output: {
filename: "index.js",
path: path.resolve(__dirname, "public")
}
}
module.exports = merge(baseConfig, clientConfig)
`
-webpack.server.js
`
const path = require("path");
const nodeExternals = require("webpack-node-externals")
const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const serverConfig = {
target: "node",//
mode: "development",
entry: "./src/server/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "build")
},
externals: [nodeExternals()]//
}
module.exports = merge(baseConfig,serverConfig)
`
-reducer.js
`
import axios from "axios"
export const getHomeList = () => {
return (dispatch) => {
axios.get("/api/v2/movie/in_theaters").then((res)=>{
console.log( res,"res")
// dispatch(changeList(data))
}).catch((err)=>{
console.log(err,"error")
})
}
}
`
I hope the boss can help me solve the problem. Thank you
GitHub: https://github.com/yyccQQu/Re.