Giter Club home page Giter Club logo

learn-hapi's Issues

Tutorial for Catbox-Redis Caching?

Need a good and a detailed explanation of catbox and catbox-redis caching.

  1. How to use catbox?
  2. How to use catbox-redis?
  3. Simple Use case of using catbox-redis?
  4. What is the actual use case for policy?

Example:
How can we use it for caching external http responses?

{"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}

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?

Uploading files in Hapi.

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?

Add contents guide?

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).

Can't find module 'hapi'

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

Testing hapi routes with Tape

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

Basic Static File Server using Hapi and Inert

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

Add CONTRIBUTING.md File to Repo

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

Proxy Example

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);

Can't run the 'hello world' example

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.