├── .gitignore ├── README.md ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | express-router-experiments 2 | ========================== 3 | 4 | Experiments with Express 4.0's Router 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-router-experiments", 3 | "main": "server.js", 4 | "dependencies": { 5 | "express": "~4.0.0" 6 | } 7 | } -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | // server.js 2 | 3 | // BASE SETUP 4 | // ============================================== 5 | 6 | var express = require('express'); 7 | var app = express(); 8 | var port = process.env.PORT || 8080; 9 | 10 | // ROUTES 11 | // ============================================== 12 | 13 | // sample route with a route the way we're used to seeing it 14 | app.get('/sample', function(req, res) { 15 | res.send('this is a sample!'); 16 | }); 17 | 18 | // we'll create our routes here 19 | 20 | // get an instance of router 21 | var router = express.Router(); 22 | 23 | // route middleware that will happen on every request 24 | router.use(function(req, res, next) { 25 | 26 | // log each request to the console 27 | console.log(req.method, req.url); 28 | 29 | // continue doing what we were doing and go to the route 30 | next(); 31 | }); 32 | 33 | // home page route (http://localhost:8080) 34 | router.get('/', function(req, res) { 35 | res.send('im the home page!'); 36 | }); 37 | 38 | // about page route (http://localhost:8080/about) 39 | router.get('/about', function(req, res) { 40 | res.send('im the about page!'); 41 | }); 42 | 43 | // route middleware to validate :name 44 | router.param('name', function(req, res, next, name) { 45 | // do validation on name here 46 | // blah blah validation 47 | // log something so we know its working 48 | console.log('doing name validations on ' + name); 49 | 50 | // once validation is done save the new item in the req 51 | req.name = name; 52 | // go to the next thing 53 | next(); 54 | }); 55 | 56 | // route with parameters (http://localhost:8080/hello/:name) 57 | router.get('/hello/:name', function(req, res) { 58 | res.send('hello ' + req.name + '!'); 59 | }); 60 | 61 | // apply the routes to our application 62 | app.use('/', router); 63 | 64 | // login routes 65 | app.route('/login') 66 | 67 | // show the form (GET http://localhost:8080/login) 68 | .get(function(req, res) { 69 | res.send('this is the login form'); 70 | }) 71 | 72 | // process the form (POST http://localhost:8080/login) 73 | .post(function(req, res) { 74 | console.log('processing'); 75 | res.send('processing the login form!'); 76 | }); 77 | 78 | // START THE SERVER 79 | // ============================================== 80 | app.listen(port); 81 | console.log('Magic happens on port ' + port); 82 | --------------------------------------------------------------------------------