node.js - 如何使用 fetch 和 request react js 更改端口?
问题描述
这是我从教授的教程中获取的代码。LoginComp 只是 {用户名:'',密码:''}
const request = new Request("/users/login", {
method: "post",
body: JSON.stringify(loginComp),
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
}
});
// Send the request with fetch()
fetch(request)
.then(res => {
if (res.status === 200) {
return res.json()
}
})
.catch(error => {
console.log(error);
});
我有一个在 localhost:5000 运行的后端服务器,在 localhost:5000/users/login 有发布路由。这段代码的问题是它假设用户/登录是在 localhost 3000 所以我会在控制台中得到这个错误:
POST http://localhost:3000/users/login 404 (Not Found)
我如何使它成为 localhost:5000 而不是 3000 (客户端服务器)
解决方案
您必须将 http://localhost:5000/ 添加到您的网址。
const request = new Request("http://localhost:5000/users/login", {
method: "post",
body: JSON.stringify(loginComp),
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
}
});
推荐阅读
- sql - 如何使用 SQLAlchemy 选择 PostgreSQL 系统列?
- apify - 如何使用 PhantonJS Scraper 在 Pagefunction 中访问 Apify SDK
- javascript - 使用反应导航的状态栏颜色
- docker - 如何获取被杀死的容器的日志
- flurry - 在 Flurry Analytics 中使用 userID 跟踪用户
- rust - 比较 Cell 内部引用的非复制枚举
- azure - 从 Dynamics 365 插件访问非 Azure Web API(本地托管在 IIS 中)
- entity-framework-core - Entityframework Core Group Join 第三个参数不填充数据成员
- kotlin - Kotlin 挂起修饰符更改函数签名,但编译器报告重载错误
- r - 使用 data.table 的函数无法输出到控制台,但可以很好地分配给对象 - 怎么会?