希望使用HapiJS作为我们的API服务器.我们需要细粒度的用户权限,例如"用户A可以编辑字段B""用户C可以查看字段D"以获得给定的模型/资源.
在我们开始构建之前,我一直在寻找是否已经完成了与Hapi兼容的类似事情.
我正在尝试将我的Express应用程序迁移到hapi.js,并且我的路线出现问题.我只想要2 GET:我的索引'/',以及不是'/'的所有内容都重定向到'/'.
使用Express我有这个:
// static files
app.use(express.static(__dirname + '/public'));
// index route
app.get('/', function (req, res) {
// whatever
}
// everything that is not /
app.get('*', function(req, res) {
res.redirect('/');
});
Run Code Online (Sandbox Code Playgroud)
我有hapi.js的问题,以获得相同的行为.我的"静态路"看起来像这样:
server.route({
method: 'GET',
path: '/{path*}',
handler: {
directory: {
path: 'public',
listing: false
}
}
});
Run Code Online (Sandbox Code Playgroud)
而我的"404道路"将是:
server.route({
method: 'GET',
path: '/{path*}',
handler: function (request, reply) {
reply.redirect('/');
}
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Error: New route /{path*} conflicts with existing /{path*}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
在表达中我有这样的事情:
router.get('/foo', middlewareFunction, function (req, res) {
res.send('YoYo');
});
Run Code Online (Sandbox Code Playgroud)
hapi中间件的形式是什么?当我有这个:
server.route({
method: 'GET',
path: '/foo',
handler: function (request, reply) {
reply('YoYo');
}
})
Run Code Online (Sandbox Code Playgroud) 我正在使用Hapi.JS在Node中构建应用程序.
我有一个认证插件的类,它给了我各种各样的问题.当我尝试this从类中的方法引用时,我得到一个错误,说明this是未定义的.为什么会这样?
摘录:
class OAuth {
constructor () {}
register (server, err, next) {
this.server = server;
this.registerRoutes();
}
registerRoutes () {
console.log(this.server.route);
this.server.route([
{
method: 'POST',
path: '/oauth/token',
config: {
auth: false,
handler: function(request,reply){
console.log("test");
reply("test");
}
}
},
{
method: 'GET',
path: '/test',
config: {
auth: false,
handler: function(request,reply){
console.log("test");
reply("test");
}
}
}
]);
}
}
module.exports = new OAuth();
Run Code Online (Sandbox Code Playgroud)
在其他地方,这被称为:
const oauth = require('./oauth');
oauth.register(server);
Run Code Online (Sandbox Code Playgroud)
每次调用寄存器函数时,都会收到此错误:
TypeError: Cannot set property 'server' of …Run Code Online (Sandbox Code Playgroud) 我开始使用Hapi nodejs框架.我使用的是"hapi@17.2.0",这是我在server.js中的代码来启动应用程序.
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 3000, host: 'localhost' });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello, world!');
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
Run Code Online (Sandbox Code Playgroud)
node server.js从终端运行我的项目之后,它会抛出错误,如下所示.
/var/www/html/hello_hapi/server.js:6
server.connection({ port: 3000, host: 'localhost' });
^
TypeError: server.connection is not a function
at Object.<anonymous> (/var/www/html/hello_hapi/server.js:6:8)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at …Run Code Online (Sandbox Code Playgroud) 我需要创建动态模式以根据请求查询中的键node js使用Joi 验证器来验证我的 api请求查询。说下面提到的模式是我的有效查询。
我正在使用hapi/joi版本16.1.8
组合1
{ type: 1, firstname: 'user first name', lastname: 'user last name'}
Run Code Online (Sandbox Code Playgroud)
组合2
{ type: 2 , salary: 1000, pension: 200}
Run Code Online (Sandbox Code Playgroud)
组合3
{ type: 3 , credit: 550, debit: 100}
Run Code Online (Sandbox Code Playgroud)
如您所见,对象键因 的值而异type。如何正确处理?
我们可以使用Joi.alternatives处理两个条件,例如
const schema = Joi.alternatives().conditional(Joi.object({ type: 1 }).unknown(), {
then: Joi.object({
type: Joi.string(),
firstname: Joi.string(),
lastname: Joi.string()
}),
otherwise: Joi.object({
type: Joi.number(),
salary: Joi.any(),
pension: Joi.any()
})
});
Run Code Online (Sandbox Code Playgroud)
但是如何在 3 个条件下做到这一点?
正在使用
@hapi/hapi:^20.0.0
node.js : v10.19.0
得到以下错误堆栈
| #meetings = null;
| ^
|
| SyntaxError: Invalid or unexpected token
| at Module._compile (internal/modules/cjs/loader.js:723:23)
| at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
| at Module.load (internal/modules/cjs/loader.js:653:32)
| at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
| at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Run Code Online (Sandbox Code Playgroud)
正在使用以下代码
'使用严格';
Run Code Online (Sandbox Code Playgroud)const Hapi = require('@hapi/hapi'); const init = async () => { const server = Hapi.server({ port: 5000, host: 'localhost' }); await server.start(); console.log('Server running on %s', server.info.uri); }; process.on('unhandledRejection', (err) => { console.log(err); process.exit(1); }); init();
我正在尝试将socket.io挂钩到Hapi.js服务器.我已经测试了vanilla Node.js中的socket.io实现,一切都很好; Hapi实现的服务器端似乎工作正常,但"/socket.io/socket.io.js"资源不会提供给客户端.
我已经检查了Hapi示例,但是他们只显示了在服务器上做了什么,他们在客户端握手上的文档看起来很奇怪:他们在端口8000上有一个服务器,但是说要发布socket.io握手到8080--我甚至试过这个(看起来很不稳定,并且与其他所有socket.io实现不一致)没有运气.
谢谢!
我正在尝试为进入Hapi处理程序的JSON对象编写Joi验证.到目前为止代码看起来像这样:
server.route({
method: 'POST',
path: '/converge',
handler: function (request, reply) {
consociator.consociate(request.payload)
.then (function (result) {
reply (200, result);
});
},
config: {
validate: {
payload: {
value: Joi.object().required().keys({ knownid: Joi.object() })
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,您可以在上面的config:validate:code部分中看到Joi对象验证.进入的JSON看起来像这样.
"key": '06e5140d-fa4e-4758-8d9d-e707bd19880d-testA',
"value": {
"ids_lot_args": {
"this_id": "stuff",
"otherThign": "more data"
},
"peripheral_data": 'Sample peripheral data of any sort'
}
Run Code Online (Sandbox Code Playgroud)
在上面的JSON中,需要对象根目录的键和值,并且ids_lot_args需要调用的部分.以peripheral_data开头的部分可以在那里,也可以是任何其他JSON有效负载.没关系,只ids_lot_args需要根级别和值内部的键和值.
到目前为止,我一直在努力让Joi验证工作. 有关如何设置的任何想法?Joi的代码仓库位于https://github.com/hapijs/joi,如果想要查看它.到目前为止,我一直在尝试允许对象上的所有函数无效.
我目前正在HapiJS/NodeJS中使用Joi来验证数据.一个POST特别具有两个ISO日期(开始日期和结束日期),这两个日期被传递到路线并经过验证以确保它们是ISO日期.
{
method: 'POST',
path: '/api/calendar',
handler: calendar.getInfo,
config: {
validate: {
payload: {
start: Joi.date().iso(),
end: Joi.date().iso()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过这两个日期并在控制器中进行一些验证,以确保开始日期在结束日期之前(即,开始日期不能是2月,结束日期是1月).
我的问题是,如果有一种方法让Joi确定这个并且出错了吗?如果需要更多信息,请与我们联系.
谢谢!Ť
hapijs ×10
node.js ×8
javascript ×4
joi ×2
validation ×2
acl ×1
babeljs ×1
ecmascript-6 ×1
json ×1
permissions ×1
redirect ×1
socket.io ×1