npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-cookie

v0.1.0

Published

Creates cookies for the hapi framework.

Downloads

18

Readme

#Hapi-cookie ##Welcome to my first hapi module

hapi-cookie simply sets a cookie in the response before it goes out.

I developed this module because I was having problems setting cookies. Hapi provides the server.state / plugin.state to set a "state" cookie which works perfectly. The problem was that I couldn't set things like expiration, path, domain etc.. just a name and value. So, I started looking for a way to do this on my own.

##How does hapi-cookie work?

The server.ext seemed like a logical place to start. There, I trigger off the "onPreResponse" event and extend "request.response.headers" with (possibly multiple) "Set-Cookie" instances. It's relatively simple and a little embarrassing how long it took me to come up with it.

##How to use this module

In your manifest.json:

{
    "servers" : [
        {
            "port": {{whatever port number}},
            "options" : {
                {{whatever options you want to set}}
            }
        }
    ],
    
    "plugins" : {
    
        "hapi-cookie" : {}
    }
}

Of course your manifest might be much larger with many more plugins and servers.

Then use the single exposed API to set a cookie:

var hapiCookie = plugin.plugins['hapi-cookie'];

/*
hapiCookie.setCookie( 
    String nameOfCookie, 
    String valueOfCookie, 
    String GMTformat, 
    String path, 
    String domain, 
    Boolean isSecure, 
    Boolean is HTTPOnly, 
    callback(error, cookie value){}
)
*/

// for example

var dur = 100000000; // add some tiem so the cookie doesn't expire right away
var dte = new Date(new Date().getTime()+dur).toUTCString();

hapiCookie.setCookie("newCookie", "cookieValue", dte, "myDomain.com", "/", true, true, function(e, cookieVal){

    if(e){ 
        console.log("The cookie was not set");
    } else {
        console.log("Cookie Set : "+cookieVal);
    }
}

Future changes:

  • There is also the possibility of using request.headers.state to attach a "Set-Cookie". Might want to fully investigate using that instead. Or give users the option.

  • Add more API's to add other headers?