我正在为一个函数编写测试代码,该函数将检查twitter中是否存在用户名.我正在使用Hapi框架和实验室进行测试.
当我运行时,npm test我收到以下错误:
> NameGen@0.0.0 test /Users/mario/projects/NameGen
> ./node_modules/lab/bin/lab -c
/Users/mario/projects/NameGen/test/test.js:5
Lab.experiment( "Test Username Existence", function() {
^
TypeError: Object #<Object> has no method 'experiment'
at Object.<anonymous> (/Users/mario/projects/NameGen/test/test.js:5:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js [as origLoader] (module.js:474:10)
at Object.require.extensions..js (/Users/mario/projects/NameGen/node_modules/lab/lib/coverage.js:32:26)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:85:23
at Array.forEach (native)
at Object.internals.traverse (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:81:19)
at Object.exports.run (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:30:29)
at Object.<anonymous> (/Users/mario/projects/NameGen/node_modules/lab/bin/lab:3:23)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用hapi.js,但有一件事对我来说并不清楚.在我使api请求在路径中传递params的情况下,我可以通过在处理程序中调用request.params来获取这些.当我以查询的形式请求应该是什么路径?在第一种情况下,我在路径属性中放置类似/ {param}但在第二种情况下?
嘿所以我使用Node.js框架Hapi.js来制作一个简单的文件上传API.收到文件后,我会将pdf文件转换为png.我一直在寻找一些关于上传文件的Hapi.js文档,但没有运气.收到文件后,我想使用Node文件系统读取它,然后将其传递给命令行工具.
这是我转换的路线:
server.route({
method: 'POST',
path: '/convert',
config: {
payload: {
output: 'stream',
parse: true,
allow: ['application/json', 'image/jpeg', 'multipart/form-data','application/pdf']
//allow:'application/json'
},
handler:function (request, reply) {
console.log(request.raw.req);
var data = request.payload;
if (data.file) {
var name = data.file.hapi.filename;
console.log(name);
var path = __dirname + "/uploads/" + name;
console.log(path);
var file = fs.createWriteStream(path);
file.on('error', function (err) {
console.error(err)
});
data.file.pipe(file);
data.file.on('end', function (err) {
var ret = {
filename: data.file.hapi.filename,
headers: data.file.hapi.headers
}
console.log(JSON.stringify(ret));
reply(JSON.stringify(ret));
});
data,file.on('data',function(err){
console.log('data');
});
}
}
} …Run Code Online (Sandbox Code Playgroud) 所以,我已经打了几天这个,我很难过,解决它的最佳方法是什么.我正在使用HAPI的Waterline/dogwater并试图做一些广泛的事情: -
wardrobe.find({WardrobeId: 5}).then(function(clothes) {
//got my clothes in my wardrobe.
clothes.find({Type: 'trousers'},{Kind: 'nice ones'}).then(function(trousers) {
//got my nice trousers
_.each(trousers, function(trouser) {
//logic to see if these are my pink trousers
console.log('color?', trouser.color);
});
console.log('ding');
});
});
Run Code Online (Sandbox Code Playgroud)
我遇到的麻烦是代码总是ding在输出裤子颜色之前.这是因为,据我所知,_.each将使代码异步.我试图介绍Promises(蓝鸟),但没有运气.我甚至查看了生成器(Co),但我的节点版本在v0.11之前修复.
我想在其中执行一些数据库查找_.each,将这些结果(如果有的话)返回到trouser对象,然后可以返回: -
wardrobe.find({WardrobeId: 5}).then(function(clothes) {
//got my clothes in my wardrobe.
clothes.find({Type: 'trousers'},{Kind: 'nice ones'}).then(function(trousers) {
//got my nice trousers
_.each(trousers, function(trouser) {
//logic to see if these are my pink trousers
db.colors.find({Color: trouser.color}).then(function(color) { …Run Code Online (Sandbox Code Playgroud) Hoek.reach解决了一个有趣且古老的javascript对象遍历问题:https://github.com/hapijs/hoek#reachobj-chain-options.
例:
var chain = 'a.b.c';
var obj = {a : {b : { c : 1}}};
Hoek.reach(obj, chain); // returns 1
Run Code Online (Sandbox Code Playgroud)
我只想要Hoek的这个功能而不是别的.只是想知道这个功能是否在Lodash?我很快就通过lodash doc进行了扫描,但似乎并不存在.
试图找出我是否有办法这样做而不添加"hoek"作为我的dep.谢谢你的帮助!
我正在使用hapi和良好的模块,我想知道在不使用控制台日志的情况下打印调试消息的最佳方法是什么.
比如现在是
server.register(
{
register: require('good'),
options: options
},
function (err) {
if (err) {
return console.error(err);
}
// If the module is required by another script, we don’t start the server (ie test suite)
if (!module.parent) {
server.start(function () {
console.info('Server started at ' + server.info.uri);
});
}
}
);
Run Code Online (Sandbox Code Playgroud)
使用https://github.com/visionmedia/debug 就好了
server.register(
{
register: require('good'),
options: options
},
function (err) {
if (err) {
return console.error(err);
}
// If the module is required by another script, we …Run Code Online (Sandbox Code Playgroud) 如何使用hapi和joi验证可选参数
path: '/users/{limit?}',
Run Code Online (Sandbox Code Playgroud)
limit是可选的,但如果存在则应该是整数.
我正在Hapi 中编写RESTFUL API,我无法弄清楚API身份验证方法.
假设我们在HTTP/1.1中使用SSL/TLS,为什么我们需要JSON Web Token(JWT),我们已经有了HTTP基本身份验证.我们可以使用HTTP Basic Auth保护每个端点,因此我们甚至不需要像这样的登录路由'/login'.
那么,OAuth和JWT的认证方案有什么意义呢?
谢谢.
我在使用和将app.css文件链接到default.html视图时遇到问题。我使用Cloud9进行托管。hapijshandlebars.js
这是我的应用程序结构:
root/
client/
config/
route.js
css/
app.css
view/
layout/
default.html
index.html
server.js
Run Code Online (Sandbox Code Playgroud)
我的 server.js
'use strict';
const Hapi = require('hapi');
const Vision = require('vision');
const Inert = require('inert');
const Path = require('path');
const Routes = require('./client/config/route')
const server = new Hapi.Server();
server.connection({
host: process.env.IP || '0.0.0.0',
port: process.env.PORT || 3000
});
server.register([Vision, Inert], (err) => {
if (err) {
throw err;
}
server.views({
engines: {
html: require('handlebars')
},
relativeTo: Path.join(__dirname, 'client'), …Run Code Online (Sandbox Code Playgroud) 查看最新的17.2.0版本的文档,在我看来,我必须显式地为每个路由添加cors支持。但是,以下内容对我不起作用
server.route({
method: 'GET',
path: path,
handler: handler,
options: {
cors: true
}
});
Run Code Online (Sandbox Code Playgroud)
而且,即使这样做,也真的想在一个地方为所有路线添加cors支持,而不是分别为每条路线添加cors支持。在以前的版本中,我可以执行以下操作
server.connection({ routes: { cors: true } });
Run Code Online (Sandbox Code Playgroud)
但这似乎不再可行。我该怎么办?