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
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
21
7.2k
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
180
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.1k
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
180
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
組織で育むオブザーバビリティ
ryota_hnk
0
180
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
140
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
ぼくの開発環境2026
yuzneri
0
220
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
910
Designing for humans not robots
tammielis
254
26k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
94
Paper Plane (Part 1)
katiecoart
PRO
0
4.2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
130
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
140
Visualization
eitanlees
150
17k
Embracing the Ebb and Flow
colly
88
5k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
730
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
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