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
私はどうやって技術力を上げたのか
yusukebe
43
18k
いま中途半端なSwift 6対応をするより、Default ActorやApproachable Concurrencyを有効にしてからでいいんじゃない?
yimajo
2
390
チームの境界をブチ抜いていけ
tokai235
0
140
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
610
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
160
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
210
Swift Concurrency - 状態監視の罠
objectiveaudio
2
490
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
170
階層構造を表現するデータ構造とリファクタリング 〜1年で10倍成長したプロダクトの変化と課題〜
yuhisatoxxx
3
960
GraphQL×Railsアプリのデータベース負荷分散 - 月間3,000万人利用サービスを無停止で
koxya
1
1.2k
SpecKitでどこまでできる? コストはどれくらい?
leveragestech
0
640
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
0
140
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Embracing the Ebb and Flow
colly
88
4.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
YesSQL, Process and Tooling at Scale
rocio
173
14k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
53k
It's Worth the Effort
3n
187
28k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
850
Building Flexible Design Systems
yeseniaperezcruz
329
39k
Practical Orchestrator
shlominoach
190
11k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
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