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
200
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Introduction to hapi
Slides from Javascript-Meetup (01. Sep 2014) in Stuttgart
Kentaro Wakayama
September 01, 2014
Other Decks in Programming
See All in Programming
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
140
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
110
今さら聞けない .NET CLI
htkym
0
170
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
350
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
630
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
140
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.7k
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
620
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
170
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
200
FDEが実現するAI駆動経営の現在地
gonta
2
240
テーブルをDELETEした
yuzneri
0
130
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
Side Projects
sachag
455
43k
Chasing Engaging Ingredients in Design
codingconduct
0
250
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
280
Optimizing for Happiness
mojombo
378
71k
Scaling GitHub
holman
464
140k
What's in a price? How to price your products and services
michaelherold
247
13k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
360
ラッコキーワード サービス紹介資料
rakko
1
4.1M
Statistics for Hackers
jakevdp
799
230k
The Cost Of JavaScript in 2023
addyosmani
55
10k
So, you think you're a good person
axbom
PRO
2
2.1k
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