Created
May 21, 2017 14:32
-
-
Save antsanchez/c1f7134187b7204cfc3e178793acdb33 to your computer and use it in GitHub Desktop.
Routes in Express.js
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() | |
function userMiddleware(req, res, next) { | |
// Aquí el código para comprobar si el usuario está logeado | |
next(); | |
} | |
function adminMiddleware(req, res, next) { | |
// Aquí el código para comprobar si es un admin logeado | |
next(); | |
} | |
app.get('/', function(req,res) { | |
res.send('Hello World!'); | |
}) | |
// Define aquí las rutas para admins | |
app.use('/admin', adminMiddleware); | |
app.get('/admin/route1', function(req,res) { | |
// Codigo aquí | |
}) | |
app.get('/admin/route2', function(req,res) { | |
// Codigo aquí | |
}) | |
// Define aquí rutas para usuarios logeados | |
app.use('/user', userMiddleware); | |
app.get('/user/route1', function(req,res) { | |
// Codigo aquí | |
}) | |
app.get('/user/route2', function(req,res) { | |
// Codigo aquí | |
}) | |
app.listen(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment