express-stickyforms
Advanced tools
Comparing version 1.0.0 to 2.0.0
43
index.js
@@ -1,16 +0,37 @@ | ||
module.exports = function(req, res, next) { | ||
module.exports = function() { | ||
if (typeof req.session.sticky === 'undefined') { | ||
res.locals.sticky = {}; | ||
} else { | ||
res.locals.sticky = req.session.sticky; | ||
delete req.session.sticky; | ||
} | ||
return function(req, res, next) { | ||
req.sticky = function() { | ||
req.session.sticky = req.body; | ||
var session = req.session; | ||
var sticky = session && session.sticky; | ||
var values = session && sticky && req.session.sticky._values; | ||
if (!values) { | ||
res.locals.sticky = {}; | ||
} else { | ||
res.locals.sticky = values; | ||
delete req.session.sticky; | ||
} | ||
req.sticky = { | ||
them: function() { req.session.sticky = { _values: req.body }; }, | ||
get: function(key) { return res.locals.sticky && res.locals.sticky[key]; } | ||
}; | ||
next(); | ||
}; | ||
next(); | ||
}; |
{ | ||
"name": "express-stickyforms", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Persistent form middleware for express.", | ||
@@ -5,0 +5,0 @@ "url": "https://github.com/emilioTe/express-stickyforms", |
134
readme.md
# express-stickyforms | ||
version **1.0.0** | ||
version **2.0.0** | ||
## Dependencies | ||
``` | ||
npm install express | ||
``` | ||
npm install express@3.x.x | ||
**Note**: Your `express` application also needs to have `session` support. | ||
Your `express` application also needs to have `session` and `bodyParser` support. | ||
## Installation | ||
npm install express-stickyforms | ||
``` | ||
npm install express-stickyforms | ||
``` | ||
@@ -20,62 +23,75 @@ | ||
### app.js | ||
```javascript | ||
var express = require('express') | ||
, app = express() | ||
; | ||
var path = require('path'); | ||
app.set('views', __dirname); | ||
app.set('view engine', 'jade'); | ||
app.set('port', 3000); | ||
app.use(express.bodyParser()); | ||
app.use(express.cookieParser('super secret')); | ||
app.use(express.session()); | ||
// | ||
// Position matters. | ||
// | ||
app.use(require('express-stickyforms')); | ||
app.use(app.router); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.get('/', function(req, res) { | ||
res.render('index'); | ||
}); | ||
app.post('/', function(req, res) { | ||
// | ||
// Tell your app that you want to be able to | ||
// redisplay the user's inputs for this form. | ||
req.sticky(); | ||
res.redirect('/'); | ||
}); | ||
require('http').createServer(app).listen(app.get('port'), function() { | ||
console.log('stickyforms app running on port %d.', app.get('port')); | ||
}); | ||
var express = require("express"); | ||
var app = express(); | ||
var bodyParser = require("body-parser"); | ||
var session = require("express-session"); | ||
var path = require("path"); | ||
app.set("views", __dirname); | ||
app.set("view engine", "jade"); | ||
app.set("port", 3000); | ||
app.use(bodyParser.urlencoded()); | ||
app.use(session()); | ||
// | ||
// Position matters. | ||
// | ||
app.use(require("express-stickyforms")()); | ||
// | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
app.get("/", function(req, res) { | ||
var stickiedValue = req.sticky.get("myValue"); | ||
res.render("index"); | ||
}); | ||
app.post("/", function(req, res) { | ||
// | ||
// Tell your app that you want to be able to | ||
// redisplay the user's inputs for this form. | ||
req.sticky.them(); | ||
res.redirect("/"); | ||
}); | ||
require("http").Server(app).listen(app.get("port"), function() { | ||
console.log("stickyforms app running on port %d.", app.get("port")); | ||
}); | ||
``` | ||
### index.jade | ||
```jade | ||
doctype html | ||
html | ||
head | ||
title stickyforms example | ||
!!! | ||
html | ||
head | ||
title stickyforms example | ||
body | ||
form(action="/", method="POST") | ||
label(for="username") Username | ||
input#username(type="text", name="username", value="#{sticky.username}") | ||
label(for="password") Password | ||
//- You should never sticky a password field! | ||
//- Doing so opens your site up to security risks. | ||
input#password(type="password", name="password") | ||
button(type="submit") Sign in | ||
body | ||
form(action='/', method='post') | ||
label(for='username') Username | ||
//- #{sticky.username || ''} | ||
//- It would be wise to structure your stickys like | ||
//- so to avoid template errors. | ||
input#username(type='text', name='username', value='#{sticky.username || ''}') | ||
label(for='password') Password | ||
//- You should never sticky a password field! | ||
//- Doing so opens your site up to security risks. | ||
input#password(type='password', name='password') | ||
button(type='submit') Sign in | ||
``` | ||
@@ -82,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4292
4
18
109