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
すべてのコンテキストを、 ユーザー価値に変える
applism118
2
1.1k
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
110
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
380
AIコーディング道場勉強会#2 君(エンジニア)たちはどう生きるか
misakiotb
1
270
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
620
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
2
460
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
250
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
880
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
680
A2A プロトコルを試してみる
azukiazusa1
2
1.3k
ふつうの技術スタックでアート作品を作ってみる
akira888
0
310
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
480
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Optimizing for Happiness
mojombo
379
70k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Designing Experiences People Love
moore
142
24k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
940
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Git: the NoSQL Database
bkeepers
PRO
430
65k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
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