dwyl / learn-hapi Goto Github PK
View Code? Open in Web Editor NEW:sunny: Learn to use Hapi.js (Node.js) web framework to build scalable apps in less time
:sunny: Learn to use Hapi.js (Node.js) web framework to build scalable apps in less time
I recently discovered a feature that I hoped would exist and was pleasantly surprised when I realised its been there all along! Now you can too!!
Any update required?
See Release notest: hapijs/hapi#2682
What are plugins and how dow I build one?
hapi simplifies testing your code. fact.
Because it now starts with the words:
UPDATE: This guide is extremely outdated!
which doesn't look great for the tutorial ๐
https://medium.com/the-spumko-suite/creating-validation-schemas-with-joi-eb4ff19f6688
We should probably add a real world Poop (error handling) example https://github.com/hapijs/poop
And maybe add https://github.com/dwyl/hapi-error too while we're at it...
How?
Both @mantagen and I have seen that you have got a hapi-upload module for file uploads. Does this mean that its not a built-in part of hapi? we are completing ex. 11 of makemehapi and found the answer to contain chunking, can you recommend any other alternatives or should we look into hapi-upload?
I think I'm ready to let go of "control" of this repo...
@iteles what do you think? ๐
As a person who is
new
to the DWYL Org/Community ๐
I need to know how to contribute to the project effectively ๐ญ
so that I can start my journey towards Doing What I Love with my Life! โค๏ธ โ ๐
Markdown:
_**Please read** our_
[**contribution guide**](https://github.com/dwyl/contributing)
(_thank you_!)
Note: these are line-separated but in the actual rendered page it's all one line.
see: https://github.com/dwyl/contributing/blob/master/CONTRIBUTING.md
I've written a hapi server and now I'm trying to test it. I've been able to write tests that pass for one of my routes but have been unsuccessful for the other two. Here are the routes I'm trying to test:
server.route([
{
method: 'GET',
path: '/',
handler: function (request, reply) {
console.log(request)
reply.file('./public/index.html')
}
},
{
method: 'GET',
path: '/s3_credentials',
handler: function (request, reply) {
if (request.query.filename) {
var filename =
crypto.randomBytes(8).toString('hex') +
path.extname(request.query.filename)
reply(s3.getS3Credentials(s3Config, filename))
} else {
reply('Filename required')
}
}
},
{
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'public',
listing: true,
index: false
}
}
}
])
And here are my tests that pass for the '/s3_credentials' route:
test('checks our /s3_credentials GET endpoint', function (t) {
var options = {
method: 'GET',
url: '/s3_credentials'
}
Server.start((err, server) => {
if (err) {
console.log(err)
}
server.inject(options, function (response) {
t.equal(response.statusCode, 200, '200 status code returned - โ
')
server.stop(t.end)
})
})
})
test('POST request to /s3_credentials should return 404', function (t) {
var options = {
method: 'POST',
url: '/s3_credentials'
}
Server.start((err, server) => {
if (err) {
console.log(err)
}
server.inject(options, function (response) {
console.log(response.result)
t.equal(response.statusCode, 404, '404 status code returned - โ
')
server.stop(t.end)
})
})
})
If anyone has any experience with testing similar routes I'd really appreciate your advice!
Thanks!
Link to the source code
When I try to run my first completed challenge "makemehapi run program.js" I get this error.
But I installed hapi with 'npm install hapi -g'
module.js:340
throw err;
^
Error: Cannot find module 'hapi'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/Users/smnplk/playground/nodejs/hapiapp/program.js:1:74)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
โ Error connecting to http://localhost:56797: ECONNREFUSED
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect as oncomplete
The Cookies
challenge has proved for me to be the most tricky challenge. I am still not sure that
I have got it right for my taste. Because of the stated cookie parsing error.
Note that these config options
are only set for the /set-cookie
route:
config: {
state: {
parse: true,
failAction: 'log'
}
}
After reading the Hapi docs about cookie parsing, I would actually expect it to be present on the /check-cookie
route in the first place as that route is responsible for the cookie validation.
However, adding it to the the /check-cookie
route invalidated the official verify
.
So, I have also added the recommended error handler for cookie parsing problems:
server.on('request-internal', (request, event, tags) => {
if (tags.error && tags.state) {
console.error("Error parsing cookie:\n", event.data);
}
});
Now, I could get a more detailed information of the cookie parsing error.
Your submission results compared to the expected:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1. ACTUAL: "[\"session=eyJrZXkiOiJtYWtlbWVoYXBpIn0=; Max-Age=0; Expires=Mon, 25 Jul 2016 11:23:27 GMT; Domain=localhost; Path=/\"]"
1. EXPECTED: "[\"session=eyJrZXkiOiJtYWtlbWVoYXBpIn0=; Max-Age=0; Expires=Mon, 25 Jul 2016 11:23:27 GMT; Domain=localhost; Path=/\"]"
Error parsing cookie:
{ header: 'session=eyJrZXkiOiJtYWtlbWVoYXBpIn0=; Max-Age=0; Expires=Mon, 25 Jul 2016 11:23:27 GMT; Domain=localhost; Path=/',
errors:
[ { name: 'Expires',
value: 'Mon, 25 Jul 2016 11:23:27 GMT',
settings: [Object],
reason: 'Invalid cookie value' } ] }
2. ACTUAL: "{\"user\":\"hapi\"}"
2. EXPECTED: "{\"statusCode\":400,\"error\":\"Bad Request\",\"message\":\"Invalid cookie value\"}"
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Submission results did not match expected
# FAIL
Your solution to COOKIES didnt pass. Try again
It looks like the Expires
key has an invalid datetime value although it looks perfectly ok to me.
Is this a bug or is it by design?
https://github.com/hapijs/lab is the "official" testing framework for Hapi.
We don't have anything against using it.
Except that when we reported a possible bug in the coverage reporter we never got a conclusive answer: hapijs/lab#401 ...
But our biggest reason for using Tape is its simplicity!
see: https://github.com/hapijs/lout
with link to how we are using it in: https://github.com/ideaq/time
Currently in the readme there are 2 lab tests in total, but the screenshots are showing that 2 tests run after writing the basic http test when only 1 has been written by following the tutorial and 3 tests after the boom section when only 2 have been written (the one that teaches use of lab and the one that teaches boom).
If we think this might confuse people, I'm happy to replace them with these images (same but show 1 test first then 2 tests):
CodeClimate is not loving this repo right now... https://codeclimate.com/github/nelsonic/learn-hapi
We need to tidy up before re-publishing the tutorial!
What are the advantages of using glue
? https://github.com/hapijs/glue
https://david-dm.org/dwyl/learn-hapi
If anyone has time to update the package.json
and send a PR please do! thanks!
The WebSocket / Socket.io Example
https://github.com/nelsonic/learn-hapi/blob/729c049edd0f46644468baeee6cdd84420a2c357/examples/socketio.js
is woefully out-of-date...
Given that Hapi has changed considerably since we last did the MakeMeHapi Workshopper,
should we spend the time to update our answers or simply delete them?
npm instal hapi inert --save-dev
create a file called static-server.js
and paste the following code:
var Hapi = require('hapi');
var Inert = require('inert'); // serve static content
var server = new Hapi.Server();
var port = process.env.PORT || 8000;
server.register(Inert, function () {
server.connection({ port: port });
server.route( {
method: 'GET',
path: '/{param*}',
handler: {
directory: { path: require('path').normalize(__dirname + '/') }
}
});
server.start(function() { console.log('Visit: http://127.0.0.1:' +port) });
}); // requires a callback function but can be blank
Now run it with node static-server.js
See release notes: hapijs/hapi#2764
I tried to run the first sample code. However, it doesn't work with an error.
To fix this problem, I replaced
var server = new Hapi.Server('0.0.0.0', 3000);
with
var server = new Hapi.Server();
server.connection({port : 3000});
I am using Windows 7 and node version is 0.10.36. I am not sure this issue results from the environment though.
A good complete beginner Hapi app which provides a service that we use every time we create a new repository is: https://github.com/dwyl/labels we could reference this in our tutorial!
Examples have been updated in commit 5d51d68, but readme has not.
Should we add a section stating who is using hapi to the readme?
http://hapijs.com/community
http://www.ethanmick.com/why-i-like-hapi-more-than-express/
thanks @indieforger for drawing our attention to this great post by @ethanmick โค๏ธ
https://github.com/nelsonic/learn-hapi/blame/master/README.md#L124-L160
have we not added npm init / npm install hapi
beacuse it is part of the intermediate section? Are we expecting the user to know that or is it worth adding it in there?
handler: function(req, reply) {
console.log(req);
reply('hello world');
}
What is the recursive output of req?
Need a good and a detailed explanation of catbox and catbox-redis caching.
Example:
How can we use it for caching external http responses?
see discussion: outmoded/discuss#66
How do we know a client supports http streams?
Draft:
var Hapi = require('hapi');
var port = process.env.PORT || 8080; // let env/heroku define port or use 8080
var server = new Hapi.Server();
var ip = require('./lib/lanip');
server.connection({ host : '0.0.0.0', port: port, routes: { cors: true } });
server.route({
method: '*',
path: '/{path*}',
handler: {
proxy: {
mapUri: function (request, callback) {
console.log(' - - - - - - - - - - - - - - - - - - - - - - request.url')
console.log(request.headers)
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
// borrowed this one-liner from: http://stackoverflow.com/questions/2992276/replace-first-character-of-string
var url = request.url.href.indexOf('/') == 0 ? request.url.href.substring(1) : request.url.href;
console.log(">> "+url)
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
callback(null,url, request.headers);
}
}
}
});
server.start();
console.log('Now Visit: http://' + ip + ':' +port);
This tutorial is ๐ฅ awesome ๐ฅ in part because it is so thorough.
I think a 'contents guide' at the beginning might help so people can not only go to parts they want but also so they have an idea ahead of time of what will be covered (things tend to click better for me that way at least).
Add this link to the Plugin section:
https://www.joyent.com/developers/videos/node-js-at-walmart-plugins-as-the-center-of-team-collaboration
We need to write a mini-example of using good
for process monitoring (logging) ...
https://github.com/hapijs/good
Not linking to the API.md is a bit of an oversight ... https://github.com/hapijs/hapi/blob/master/API.md ๐ฎ
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.