Last active
March 15, 2017 22:48
-
-
Save sorskoot/374c20f2a7313ea76bdd0e2faf222a67 to your computer and use it in GitHub Desktop.
Express.js route to success
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
router.all('*', function(req, res, next){ | |
//… | |
next(); | |
}); | |
router.get('/p/:name', function(req, res, next){ | |
//… | |
}); | |
router.param('name', function(req, res, next, name){ | |
//… | |
next(); | |
}); |
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
app.get('/', function (req, res) { | |
res.send('Hello World!') | |
}) |
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 index = require('./routes/index'); | |
// ** lots of other code in between ** // | |
app.use('/', index); |
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 router = express.Router(); | |
/* GET home page. */ | |
router.get('/', function(req, res, next) { | |
res.render('index', { title: 'Express' }); | |
}); | |
module.exports = router; |
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
router.get('/p', function(req,res){ | |
res.send(req.params.name); | |
}) |
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
router.get('/p/:name', function(req,res){ | |
res.send(req.params.name); | |
}) |
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
router.post('/p', function(req, res){ | |
res.send(req.body.name); | |
}) |
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
router.get('/feed/:url', function (req, res, next) { | |
try { | |
// do some stuff | |
res.json(result); | |
} catch (e) { | |
res.sendStatus(500, e.message); | |
} | |
}) | |
router.get('/', function(req, res, next) { | |
res.render('index', { title: 'Express' }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment