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

flash-fiction

v1.0.0

Published

Flash messages middleware for Express/Connect with support for redirects and immediate use. Useful for informational or errors messages. Like Rails's Flash.

Downloads

3

Readme

Flash Fiction.js

NPM version

Flash Fiction.js is an object for storing keys and values and later serializing unused/unread ones. It's primarily useful for storing informational and error messages in web apps for page rendering or redirects. It comes with middleware for Connect.js and Express.js that stores unused messages in signed cookies for the next request. It's similar in spirit to Rails's ActionDispatch::Flash.

The middleware works with Express v4 and assumes you've set up cookie-parser with a secret. Another cookie parser library will work, too, if it has an equivalent API (res.signedCookies).

Installing

npm install flash-fiction

Flash Fiction.js follows semantic versioning, so feel free to depend on its major version with something like >= 1.0.0 < 2 (a.k.a ^1.0.0).

Using

var Express = require("express")
var app = Express()
app.use(require("cookie-parser")(COOKIE_SECRET))
app.use(require("flash-fiction/express"))

After the Flash middleware has run, you'll have flash functions available on the request and response objects for getting and setting messages respectively.

In your HTTP handler, call res.flash to set a message (you can pass it any JSON-compatible value):

app.post("/models", function(req, res) {
  // ... Something that saves the model.
  res.flash("info", "Created successfully.")
  res.redirect("/models/42")
})

Then once your client's browser follows the redirect, you have req.flash to read the value back out:

app.get("/models/:id", function(req, res) {
  if (req.flash("info")) console.log(req.flash("info"))
  res.render("models/read")
})

The getter function is also available as a local variable in templates.
For example, a Jade/Pug template would look like the following:

if flash("error")
  h1 Uh-oh!
  p= flash("error")

Accessing Immediately Without Redirect

Sometimes you don't need to do a redirect, but would still like to use the flash object for passing messages to the view. Set and get flash messages as before with res.flash("info", msg) and req.flash("info"). You can access set messages immediately on req.flash. Flash Fiction.js is even also smart enough to then not serialize that used key for the next request.

Serializing for JavaScript

If you'd like to pass the flash messages from Node's side to client side JavaScript, you can call flash without any arguments in your template to get all the messages back.

script window.flash = #{JSON.stringify(flash()).replace(/<\//g, "<\\/")}

Remember to escape </ lest you create a security vulnerability due to in-band signaling of one <script> tag's end and the other's beginning.

Using without Connect/Express

If you'd like to use the Flash object yourself without the middleware, require it:

var Flash = require("flash-fiction")
var flash = new Flash({info: "OK"})
flash.set("notice", "OMG")
flash.get("info") // => "OK"
flash.get() // => {info: "OK", notice: "OMG"}

License

Flash Fiction.js is released under a Lesser GNU Affero General Public License, which in summary means:

  • You can use this program for no cost.
  • You can use this program for both personal and commercial reasons.
  • You do not have to share your own program's code which uses this program.
  • You have to share modifications (e.g. bug-fixes) you've made to this program.

For more convoluted language, see the LICENSE file.

About

Andri Möll typed this and the code.
Monday Calendar supported the engineering work.

If you find Flash Fiction.js needs improving, please don't hesitate to type to me now at [email protected] or create an issue online.