- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个使用 React 的客户端和一个使用 HapiJS 的服务器。
在客户端中,我尝试向服务器发出 POST 请求,但出现以下错误
OPTIONS http://localhost:3001/sentilize/ 404 (Not Found) localhost/:1 Failed to load http://localhost:3001/sentilize/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
我在端口 3001 上运行服务器。我在 3000 上运行客户端。
如有任何帮助/指导,我们将不胜感激。此外,我可以使用 POSTMAN 成功地向服务器发出发布请求。它只是来自 React,它不起作用。
这是我的 server.js
import Hapi from "hapi";
import { routes } from "./routes";
const server = Hapi.server({
host: "localhost",
port: 3001,
routes: { cors: true }
});
// Register Routes
routes.forEach(route => {
server.route(route);
});
// We need to install babel-polyfill
// because async and await are in es7
async function start() {
try {
await server.start();
} catch (err) {
console.log(err);
}
console.log("Hapii server is running");
}
start();
这是在前端进行调用的代码
fetchSentiment = () => {
axios
.post("/", {
sentence: this.state.value
})
.then(response => {
this.setState({ sentiment: response.data });
})
.catch(error => {
console.log("Error: ", error);
});
};
我有一个 axios 实例设置如下 (axios-sentiment.js)
import axios from "axios";
const instance = axios.create({
baseURL: "http://localhost:3001/sentilize"
});
export default instance;
最佳答案
服务器配置看起来正确。有可能正在使用的客户端 HTTP 库没有发送正确的 header 。特别是,确保请求具有 Origin
header 。请注意,cors: true
设置仅支持某些 CORS 场景,并非在所有情况下都有效。了解 simplified requests了解 CORS 的一些细微差别。然后 route.options.cors文档将更有意义,如果需要,您可以将其设置为调整其行为的对象。
一些其他不相关的提示:
async
await
在 Node 7.6 及更高版本中受支持。您可能根本不需要 babel 或 babel-polyfill。参见 node.green了解支持哪些语言功能。关于node.js - CORS 问题,HapiJS HAPI-16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331473/
我想向 hapi 服务器添加插件,该服务器具有多个连接,例如监听不同的 ip。 是否可以向所有配置的服务器添加插件? 或者如何循环所有服务器以将插件添加到所有服务器? 最佳答案 默认情况下,插件会在调
如何验证可选参数使用 hapi 和 joi path: '/users/{limit?}', limit 是可选的,但如果存在则应为整数。 最佳答案 您可以使用 Joi.number().intege
我想允许我的前端将值的哈希值推送到后端。哈希可以具有任何(JSON 合法)键,但每个键的值必须符合特定模式。可以这样想: extended: { tubers: { potato: {we
如何使用 Hapi 18.1 设置响应延迟,如果 API 响应缓慢,我想看到沙漏旋转。在早期版本中,我使用了如下所示的 setTimeout()。 server.route({ method:
我使用的是hapi,而不是express。 super 测试还应该有效吗? 如果是这样,有没有一种快速的方法来更改我的代码以使其运行? 我的测试看起来像这样,基于 documentation : im
根据文档https://hapijs.com/api/16.0.1#route-configuration路由方法可能不是“HEAD”类型,但我不明白为什么我不能覆盖该行为。 最佳答案 HEAD 路由
基本上,我的应用有一个登录/注册登录页面。用户登录或注册后,我不希望他们能够再访问此路由。我不确定如何使用 Hapi.js 实现这一目标。登录页面不使用任何身份验证策略,因此它不知道用户是否登录。 最
我查看了为 FHIR 创建配置文件的文档,并且使用 Forge 创建了一些我认为可以有效描述配置文件的 xml。我在文档中找不到它实际说明我需要向 RESTful api 发出什么请求才能实际添加新配
我在 hapi.js 中使用访问 token 时遇到问题。我无法理解如何使用该 token 进行身份验证。我正在关注这篇文章 dwyl/hapi-auth-jwt2 .我正在使用 mongodb 作为
我刚刚开始学习hapi.js 。我正在尝试理解身份验证的代码。这是如何运作的。为此,我关注了hapi-auth-jwt2 。 之后,当我从 postman 调用 API 时,我没有得到任何输出。 这是
正在使用 @hapi/hapi : ^20.0.0 Node .js:v10.19.0 得到以下错误堆栈 | #meetings = null; | ^ | | SyntaxError
我一年前使用以下版本的 hapi 和 hapi-swagger 创建了一个 REST API。 hapi: "8.5.1", hapi-swagger: "0.7.3" 为了注册 hapi-swagg
我们目前使用 HAPI 和 HAPI-AUTH-BASIC 进行身份验证。 配置路由时,是否可以检查用户是否已通过身份验证,如果未通过身份验证,则将其重定向到自定义登录页面? 每当我们将身份验证设置为
我需要在 server.onPreHandler ext 方法中设置一些响应 header 。有两种情况,当用户向我的路线终点发送 API 请求时,我需要发生这种情况。1)在成功的场景中,我需要设置标
我正在构建一个简单的日志应用程序,并尝试实现模块化以使代码更具可读性。然而,当我启动我的应用程序时遇到了一些麻烦。我有两个文件:index.js,其中有我的服务器配置;routes.js,其中有我的路
可以将代理对象添加到 Hapi 上的预处理程序中,例如 pre:[{ assign: 'callProxy' method: { proxy:{}
尝试验证数组在一种情况下是否有零个或多个字符串,以及在另一种情况下是否有零个或多个对象,与 Joi 文档作斗争:( validate: { headers: Joi.object({
我正在尝试将 hapi.js 与 mysql 连接起来。但是在定义一个 server.route 的时候。 处理程序 没有返回值。 server.route({ method:'GET
我希望在数据库响应之前向我的用户显示加载图标,然后将其重定向到正确的页面。 我的代码是这样的: // handler function handler(request, reply) { repl
在 hapi.js 中,我可以使用将请求从一条路由重定向到另一条路由 response.redirect('/home') 如何通过重定向发送一些数据?我尝试在标题中设置它 response.redi
我是一名优秀的程序员,十分优秀!