Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flash instance returns middleware directly without executing #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
node_js:
- "0.8"
- "0.10"
- "0.11"
- "7"
- "8"
- "9"
- "10"
- "11"
language: node_js
script: "npm run-script test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,47 @@ The simplest flash implementation for Express.
## Usage

```bash
npm i flash
npm install flash
```

```js
app.use(session()); // session middleware
app.use(require('flash')());
let flash = require('flash')

app.use(function (req, res) {
app.use(session()) // session middleware
app.use(flash)

app.use(function (req, res, next) {
// flash a message
req.flash('info', 'hello!');
next();
req.flash('info', 'hello!')
next()
})
```
## Using Pug

```jade
```pug
for message in flash
a.alert(class='alert-' + message.type)
p= message.message
```

```jade
while message = flash.shift() // consume messages as jade reads them
Or

```pug
while message = flash.shift() // consume messages as pug reads them
a.alert(class='alert-' + message.type)
p= message.message
```

## Using Nunjucks

```nunjucks
{% set data = flash.shift() %}

<div class="card">
<p class="text"><strong>{{data.type}}:</strong> {{ data.message }}</p>
</div>
```

## API

### req.flash([type], msg)
Expand Down
47 changes: 26 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
'use strict'

var assert = require('assert')

module.exports = function () {
return function (req, res, next) {
assert(req.session, 'a req.session is required!')
function flash (req, res, next) {
// Let's validate that req.session is required
if (req.session) {
// If session.flash doesn't exists we create a new array
if (!Array.isArray(req.session.flash)) req.session.flash = []

res.locals.flash = req.session.flash
req.flash = res.flash = push
req.flash = res.flash = pushMessage

next()
} else {
let error = new Error('A req.session is required!')

next(error)
}
}

function push(type, msg) {
if (!msg) {
msg = type
type = 'info'
}
msg = {
message: msg,
type: type
function pushMessage (type, message) {
let res = this.res || this
let messages = res.locals.flash
// Let's create a new flash message to push
let flashMessage = {
type: message ? type : 'info',
message: message || type
}
var res = this.res || this
var messages = res.locals.flash

// do not allow duplicate flash messages
for (var i = 0; i < messages.length; i++) {
var message = messages[i]
if (msg.type === message.type && msg.message === message.message) return this
}
messages.push(msg)
let msg = messages.find(msg => msg.type === flashMessage.type && msg.message === flashMessage.message)
if (msg) return this

messages.push(flashMessage)
return this
}

module.exports = flash
Loading