Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to hapi
Search
Kentaro Wakayama
September 01, 2014
Programming
3
200
Introduction to hapi
Slides from Javascript-Meetup (01. Sep 2014) in Stuttgart
Kentaro Wakayama
September 01, 2014
Tweet
Share
Other Decks in Programming
See All in Programming
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.1k
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
CSC307 Lecture 08
javiergs
PRO
0
670
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
190
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
120
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1k
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
700
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
620
Oxlintはいいぞ
yug1224
5
1.3k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
CSC307 Lecture 01
javiergs
PRO
0
690
Featured
See All Featured
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
76
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
120
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
62
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Bash Introduction
62gerente
615
210k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Being A Developer After 40
akosma
91
590k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
1
52
Building Adaptive Systems
keathley
44
2.9k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
53
The Spectacular Lies of Maps
axbom
PRO
1
520
Transcript
Introduc)on*to*hapi @wakayamakentaro
A"rich"framework"for"building" applica5ons"and"services —"hapi
Stats Current'version:'v6.7.1 Downloads'last'month:'40,819
Ge#ng&Started var hapi = require('hapi'), server = new hapi.Server('localhost', 8000);
server.start(function() { console.log('hapi server started @ ' + server.info.uri); });
Rou$ng server.route({ method: 'GET', path: '/hello/{name*2}', handler: function(request, reply) {
reply('hello ' + request.params.name + '. Nice to see you!'); } });
Caching server.method('getProfile', function(id, next) { next(null, { 'id': id, 'timestamp':
new Date }); }, { cache: { expiresIn: 10000 } }); server.route({ method: 'GET', path: '/profile/{id}', handler: function(request, reply) { server.methods.getProfile(request.params.id, reply); } });
Valida&on)with)Joi server.route({ method: 'GET', path: '/hello/{name}', handler: function(request, reply) {
reply('hello ' + request.params.name + '. Nice to see you!'); }, config: { validate: { params: { name: joi.string().required().min(3).max(10) } } } });
Plugins()(Modular(Concept var hapi = require('hapi'); hapiSwagger = require('hapi-swagger'); var server
= new hapi.Server('localhost', 8000); server.pack.register(hapiSwagger, function(err) { if (err) throw err; server.start(function() { console.log('hapi server started @ ' + server.info.uri); }); });
Plugins()(Basic(structure exports.register = function (plugin, options, next) { plugin.route({ method:
'GET', path: '/stuttgartjs', handler: function (request, reply) { reply('pizza and beer!'); } }); next(); }; exports.register.attributes = { pkg: require('./package.json'); };
Tes$ng var assert = require('assert'), hapi = require('hapi'), plugin =
require('../'); describe('Test Plugin', function() { var server; beforeEach(function(){ server = new hapi.Server(); }); it('loads successfully', function(done) { server.pack.register(plugin, function(err) { assert.ok(!err); done(); }); }); it('responses to GET request on /', function(done) { var request = { method: 'GET', url: '/' }; server.pack.register(plugin, function(err) { server.inject(request, function(res) { assert.equal(res.statusCode, 200); assert.equal(res.result, 'don\'t worry, be hapi!'); done(); }); }); }); });
Live%Coding%+%Create%a%hapi%plugin Steps: 1. Listens)to)a)sum$route. 2. Accepts)two$parameters 3. Has)valida0on 4. Is)tested
Use$generator+hapi+plugin npm i -g generator-hapi-plugin
Composer var manifest = { servers: [ { port: 8080
}, { port: 8081 } ], plugins: { "hapi-swagger": {} "./plugins/example": { endPoint: "/my/custom/route" } } }; hapi.Pack.compose(manifest, function(err, pack) { pack.start(function() { console.log('Servers started'); }); });
Composer(commandline manifest.json,+,package.json { "servers": [{ "port": 8080 }, { "port":
8081 }], "plugins": { "hapi-swagger": {}, "stuttgartjs": {} } } Use$hapi$globally hapi -c manifest.json
QA @wakayamakentaro