Last active
October 29, 2015 07:39
-
-
Save crguezl/fa0e8d6c02e8ba573494 to your computer and use it in GitHub Desktop.
Ejemplo de middleware expressJS: https://www.youtube.com/watch?v=2r6BU7R2RYI&feature=youtu.be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
app.get('/user/:id', function(req, res, next){ | |
if (req.params.id.match(/^(eva|ana)$/i)) { | |
res.send('usuario del sistema'); | |
} | |
else { | |
next(new Error('usuario desconocido')); | |
} | |
}); | |
app.get('*', function(req,res){ | |
res.send('operación inválida'); | |
}); | |
app.use(function(err, req, res, next) { | |
res.send('Mensaje: '+err.toString()); | |
}); | |
app.listen(3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
app.get('/user/:id', function(req,res,next) { | |
if (req.params.id === 'ana' || req.params.id === 'eva') { | |
res.send('usuario del sistema'); | |
} | |
else next(); | |
}); | |
app.get('*', function(req, res){ | |
res.send('usuario desconocido'); | |
}); | |
app.listen(3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
app.locals.counter = 5; | |
app.use(function(req, res, next) { | |
app.locals.counter += 1; | |
console.log(req.path); | |
console.log(app.locals.counter); | |
next(); | |
}); | |
app.get('*', function(req, res){ | |
res.send("Number of visits: "+app.locals.counter); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Véase:
Se trata de una ligera modificación del ejemplo