我的问题可能是重复的,但是我还没有找到解决方案。
我使用express.js设置了本地HTTP服务器:server.js
app.use(bodyParser.json())
app.get('/todo', function(req, res) { /**some code**/}
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
当我查询http://localhost:3000/todo时,我会收到正确的答复。
然后我运行一个webpack-dev-server:webpack.config.js
devServer: {
hot: true,
proxy: {
"/api": {
target: "http://localhost:3000",
pathRewrite: {'^/api' : ''}
}
}
}`
在我的前端代码中,我调用以下函数:
axios.get(/api/todo/, {
})
.then((response) => {
return response.data;
})
我得到一个空的html响应,尽管预期的答案是使用http://localhost:3000/todo收到的json。
我认为问题出在我的webpack配置中。因此,我尝试将"/api"
替换为"/api/*"
或"^/api/*"
,但似乎无济于事。
奇怪的是,在尝试使用get
,post
,put
编程CRUD Express服务器之前,我只使用了app.post('/', function(req, res) { /**some code**/}
来避免出现问题。和app.put('/', function(req, res) { /**some code**/}
放在我的server.js中,并通过HTTP查询中的前端传递每个参数。
我使用:
你有什么主意吗?
谢谢。