Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 0ec0f5118e2840cb96ba5049bdd2c380e48c7a52 0 parents
@jaredhanson authored
2  .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
8 .npmignore
@@ -0,0 +1,8 @@
+*.md
+.DS_Store
+.git*
+Makefile
+docs/
+examples/
+support/
+test/
20 LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2011 Jared Hanson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 Makefile
@@ -0,0 +1,19 @@
+NODE = node
+TEST = vows
+TESTS ?= test/*-test.js
+
+test:
+ @NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)
+
+docs: docs/api.html
+
+docs/api.html: lib/connect-flash/*.js
+ dox \
+ --title connect-flash \
+ --desc "Flash message middleware for Connect" \
+ $(shell find lib/connect-flash/* -type f) > $@
+
+docclean:
+ rm -f docs/*.{1,html}
+
+.PHONY: test docs docclean
1  README.md
@@ -0,0 +1 @@
+# connect-flash
39 examples/express2/app.js
@@ -0,0 +1,39 @@
+var express = require('express')
+ , flash = require('../..')
+ , util = require('util');
+
+
+var app = express.createServer();
+
+// configure Express
+app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'ejs');
+ app.use(express.logger());
+ app.use(express.cookieParser());
+ app.use(express.session({ secret: 'keyboard cat' }));
+
+ // Setting `unsafe` to `true` causes connect-flash's implementation to
+ // override Express 2.x's implementation. Functionally these are equivalent,
+ // so there is no reason to use connect-flash with Express 2.x. This example
+ // is for illustrative purposes only.
+
+ app.use(flash({ unsafe: true }));
+ app.use(app.router);
+});
+
+
+app.get('/', function(req, res){
+ res.render('index', { message: req.flash('info') });
+});
+
+app.get('/flash', function(req, res){
+ req.flash('info', 'Hi there!')
+ res.redirect('/');
+});
+
+app.get('/no-flash', function(req, res){
+ res.redirect('/');
+});
+
+app.listen(3000);
8 examples/express2/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "connect-flash-examples-express2",
+ "version": "0.0.0",
+ "dependencies": {
+ "express": "2.x.x",
+ "ejs": "0.6.x"
+ }
+}
8 examples/express2/views/index.ejs
@@ -0,0 +1,8 @@
+<p>
+<a href="/flash">Flash</a> |
+<a href="/no-flash">No Flash</a>
+</p>
+
+<% if (message) { %>
+<p><%= message %></p>
+<% } %>
9 examples/express2/views/layout.ejs
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Express 2.x Example for connect-flash</title>
+ </head>
+ <body>
+ <%- body %>
+ </body>
+</html>
37 examples/express3/app.js
@@ -0,0 +1,37 @@
+var express = require('express')
+ , flash = require('../..')
+ , util = require('util');
+
+
+var app = express();
+
+// configure Express
+app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'ejs');
+ app.use(express.logger());
+ app.use(express.cookieParser('keyboard cat'));
+ app.use(express.session({ key: 'sid', cookie: { maxAge: 60000 }}));
+
+ // Use connect-flash middleware. This will add a `req.flash()` function to
+ // all requests, matching the functionality offered in Express 2.x.
+
+ app.use(flash());
+ app.use(app.router);
+});
+
+
+app.get('/', function(req, res){
+ res.render('index', { message: req.flash('info') });
+});
+
+app.get('/flash', function(req, res){
+ req.flash('info', 'Hi there!')
+ res.redirect('/');
+});
+
+app.get('/no-flash', function(req, res){
+ res.redirect('/');
+});
+
+app.listen(3000);
8 examples/express3/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "connect-flash-examples-express3",
+ "version": "0.0.0",
+ "dependencies": {
+ "express": "git://github.com/visionmedia/express.git",
+ "ejs": "0.6.x"
+ }
+}
18 examples/express3/views/index.ejs
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Express 3.x Example for connect-flash</title>
+ </head>
+ <body>
+
+<p>
+<a href="/flash">Flash</a> |
+<a href="/no-flash">No Flash</a>
+</p>
+
+<% if (message) { %>
+<p><%= message %></p>
+<% } %>
+
+ </body>
+</html>
28 lib/flash.js
@@ -0,0 +1,28 @@
+module.exports = function flash(options) {
+ options = options || {};
+ var safe = (options.unsafe === undefined) ? true : !options.unsafe;
+
+ return function(req, res, next) {
+ if (req.flash && safe) { return next(); }
+
+ req.flash = _flash;
+ next();
+ }
+}
+
+function _flash(type, msg) {
+ if (this.session === undefined) throw Error('req.flash() requires sessions');
+ var msgs = this.session.flash = this.session.flash || {};
+ if (type && msg) {
+ // TODO: Format messages
+
+ return (msgs[type] = msgs[type] || []).push(msg);
+ } else if (type) {
+ var arr = msgs[type];
+ delete msgs[type];
+ return arr || [];
+ } else {
+ this.session.flash = {};
+ return msgs;
+ }
+}
4 lib/index.js
@@ -0,0 +1,4 @@
+/**
+ * Expose middleware.
+ */
+exports = module.exports = require('./flash');
28 package.json
@@ -0,0 +1,28 @@
+{
+ "name": "connect-flash",
+ "version": "0.0.0",
+ "description": "Flash message middleware for Connect.",
+ "author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jaredhanson/connect-flash.git"
+ },
+ "bugs": {
+ "url": "http://github.com/jaredhanson/connect-flash/issues"
+ },
+ "main": "./lib",
+ "dependencies": {
+ },
+ "devDependencies": {
+ "vows": "0.6.x"
+ },
+ "scripts": {
+ "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
+ },
+ "engines": { "node": ">= 0.4.0" },
+ "licenses": [ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/MIT"
+ } ],
+ "keywords": ["connect", "express", "flash", "messages"]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.