Skip to content
Myr

deprecate .createServer() & remove old stale examples

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 2d91eac · Browse files · 19 files+136 added−310 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

19 files+136 added−310 removed

  • other:+136 added−310 removed

19 files+136 added−310 removed

modified:examples/jade/index.js+2 added-3 removed
deleted:examples/partials/views/ninja/victim/index.jade-5 removed
1// this is insane overkill, I do not recommend2// doing tiny partials like this as it gets expensive3// with collections, however this illustrates the new4// partial lookup mechanism5!= partial('../../li', { object: victim, as: 'value' })
deleted:examples/partials/views/ninja/_weapon.jade-1 removed
1li.weapon= weapon
deleted:examples/partials/views/ninja/index.jade-22 removed
1h1= ninja.name23// file, partial name, and partial object all match ('summary')4// the partial filename prefix '_' is completely optional.56// In this case we need to specify ninja.summary as the object7// option, since it is a "plain" object Express cannot otherwise8// tell if it is intended to be locals, or THE summary object9#summary!= partial('summary', { object: ninja.summary })1011// file, partial name = '_weapon', resolves to 'weapon' object within partial12#weapons13 h2 Weapons14 // the weapon partial is rendered once per item in15 // the weapons array or "collection"16 ul!= partial('weapon', ninja.weapons)17 18// partial name 'victim' resolves to 'victim.jade'19// or 'victim/index.jade', providing the "victim" local20#victims21 h2 Victims22 ul!= partial('victim', ninja.victims)
deleted:examples/partials/views/ninja/summary.jade-4 removed
1h2 Summary2p= summary.email3p= summary.description4p taught by master #{summary.master}
deleted:examples/partials/views/layout.jade-5 removed
1!!!2html3 head4 title Partials Example5 body!= body
deleted:examples/partials/views/li.jade-1 removed
1li= value
deleted:examples/partials/app.js-32 removed
deleted:examples/route-middleware/app.js-85 removed
1/**2 * Module dependencies.3 */45var express = require('../../lib/express');67var app = express.createServer();89// Example requests:10// curl http://localhost:3000/user/011// curl http://localhost:3000/user/0/edit12// curl http://localhost:3000/user/113// curl http://localhost:3000/user/1/edit (unauthorized since this is not you)14// curl -X DELETE http://localhost:3000/user/0 (unauthorized since you are not an admin)1516// Dummy users17var users = [18 { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' }19 , { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' }20 , { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }21];2223function loadUser(req, res, next) {24 // You would fetch your user from the db25 var user = users[req.params.id];26 if (user) {27 req.user = user;28 next();29 } else {30 next(new Error('Failed to load user ' + req.params.id));31 }32}3334function andRestrictToSelf(req, res, next) {35 // If our authenticated user is the user we are viewing36 // then everything is fine :)37 if (req.authenticatedUser.id == req.user.id) {38 next();39 } else {40 // You may want to implement specific exceptions41 // such as UnauthorizedError or similar so that you42 // can handle these can be special-cased in an error handler43 // (view ./examples/pages for this)44 next(new Error('Unauthorized'));45 }46}4748function andRestrictTo(role) {49 return function(req, res, next) {50 if (req.authenticatedUser.role == role) {51 next();52 } else {53 next(new Error('Unauthorized'));54 }55 }56}5758// Middleware for faux authentication59// you would of course implement something real,60// but this illustrates how an authenticated user61// may interact with middleware6263app.use(function(req, res, next){64 req.authenticatedUser = users[0];65 next();66});6768app.get('/', function(req, res){69 res.redirect('/user/0');70});7172app.get('/user/:id', loadUser, function(req, res){73 res.send('Viewing user ' + req.user.name);74});7576app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){77 res.send('Editing user ' + req.user.name);78});7980app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){81 res.send('Deleted user ' + req.user.name);82});8384app.listen(3000);85console.log('Express app started on port 3000');
added:examples/route-middleware/index.js+85 added
1/**2 * Module dependencies.3 */45var express = require('../../lib/express');67var app = express();89// Example requests:10// curl http://localhost:3000/user/011// curl http://localhost:3000/user/0/edit12// curl http://localhost:3000/user/113// curl http://localhost:3000/user/1/edit (unauthorized since this is not you)14// curl -X DELETE http://localhost:3000/user/0 (unauthorized since you are not an admin)1516// Dummy users17var users = [18 { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' }19 , { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' }20 , { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }21];2223function loadUser(req, res, next) {24 // You would fetch your user from the db25 var user = users[req.params.id];26 if (user) {27 req.user = user;28 next();29 } else {30 next(new Error('Failed to load user ' + req.params.id));31 }32}3334function andRestrictToSelf(req, res, next) {35 // If our authenticated user is the user we are viewing36 // then everything is fine :)37 if (req.authenticatedUser.id == req.user.id) {38 next();39 } else {40 // You may want to implement specific exceptions41 // such as UnauthorizedError or similar so that you42 // can handle these can be special-cased in an error handler43 // (view ./examples/pages for this)44 next(new Error('Unauthorized'));45 }46}4748function andRestrictTo(role) {49 return function(req, res, next) {50 if (req.authenticatedUser.role == role) {51 next();52 } else {53 next(new Error('Unauthorized'));54 }55 }56}5758// Middleware for faux authentication59// you would of course implement something real,60// but this illustrates how an authenticated user61// may interact with middleware6263app.use(function(req, res, next){64 req.authenticatedUser = users[0];65 next();66});6768app.get('/', function(req, res){69 res.redirect('/user/0');70});7172app.get('/user/:id', loadUser, function(req, res){73 res.send('Viewing user ' + req.user.name);74});7576app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){77 res.send('Editing user ' + req.user.name);78});7980app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){81 res.send('Deleted user ' + req.user.name);82});8384app.listen(3000);85console.log('Express app started on port 3000');
deleted:examples/route-separation/app.js-38 removed
12/**3 * Module dependencies.4 */56var express = require('../../lib/express')7 , app = express.createServer()8 , site = require('./site')9 , post = require('./post')10 , user = require('./user');1112// Config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');16app.use(express.cookieParser());17app.use(express.methodOverride());18app.use(express.static(__dirname + '/public'));1920// General2122app.get('/', site.index);2324// User2526app.all('/users', user.list);27app.all('/user/:id/:op?', user.load);28app.get('/user/:id', user.view);29app.get('/user/:id/view', user.view);30app.get('/user/:id/edit', user.edit);31app.put('/user/:id/edit', user.update);3233// Posts3435app.get('/posts', post.list);3637app.listen(3000);38console.log('Express app started on port 3000');
added:examples/route-separation/index.js+38 added
12/**3 * Module dependencies.4 */56var express = require('../..')7 , app = express()8 , site = require('./site')9 , post = require('./post')10 , user = require('./user');1112// Config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');16app.use(express.cookieParser());17app.use(express.methodOverride());18app.use(express.static(__dirname + '/public'));1920// General2122app.get('/', site.index);2324// User2526app.all('/users', user.list);27app.all('/user/:id/:op?', user.load);28app.get('/user/:id', user.view);29app.get('/user/:id/view', user.view);30app.get('/user/:id/edit', user.edit);31app.put('/user/:id/edit', user.update);3233// Posts3435app.get('/posts', post.list);3637app.listen(3000);38console.log('Express app started on port 3000');
deleted:examples/say/app.js-44 removed
12/**3 * Module dependencies.4 */56var express = require('../../')7 , path = require('path')8 , exec = require('child_process').exec9 , fs = require('fs');1011/**12 * Error handler.13 */1415function errorHandler(voice) {16 return function(err, req, res, next) {17 var parts = err.stack.split('\n')[1].split(/[()]/)[1].split(':')18 , filename = parts.shift()19 , basename = path.basename(filename)20 , lineno = parts.shift()21 , col = parts.shift()22 , lines = fs.readFileSync(filename, 'utf8').split('\n')23 , line = lines[lineno - 1].replace(/\./, ' ');2425 exec('say -v "' + voice + '" '26 + err.message27 + ' on line ' + lineno28 + ' of ' + basename + '.'29 + ' The contents of this line is '30 + ' "' + line + '".');3132 res.send(500);33 }34}3536var app = express.createServer();3738app.get('/', function(request, response){39 if (request.is(foo)) response.end('bar');40});4142app.use(errorHandler('Vicki'));4344app.listen(3000);
deleted:examples/stylus/public/.gitignore-1 removed
1*.css
deleted:examples/stylus/views/index.jade-2 removed
1h1 Stylus2p Just an example of using Stylus with Express.
deleted:examples/stylus/views/layout.jade-6 removed
1html2 head3 title Stylus Example4 link(rel='stylesheet', href='/reset.css')5 link(rel='stylesheet', href='/main.css')6 body!= body
deleted:examples/stylus/views/main.styl-8 removed
12body3 font 14px helvetica, arial, sans-serif4 padding 50px5 h16 font-size 50px7 p8 margin 15px 0
deleted:examples/stylus/app.js-51 removed
12/**3 * Module dependencies.4 */56var express = require('../../lib/express')7 , stylus = require('stylus');89var app = express.createServer();1011// $ npm install stylus1213// completely optional, however14// the compile function allows you to15// define additional functions exposed to Stylus,16// alter settings, etc1718function compile(str, path) {19 return stylus(str)20 .set('filename', path)21 .set('compress', true);22};2324// add the stylus middleware, which re-compiles when25// a stylesheet has changed, compiling FROM src,26// TO dest. dest is optional, defaulting to src2728app.use(stylus.middleware({29 src: __dirname + '/views'30 , dest: __dirname + '/public'31 , compile: compile32}));333435// minimal setup both reading and writting to ./public36// would look like:37// app.use(stylus.middleware({ src: __dirname + '/public' }));3839// the middleware itself does not serve the static40// css files, so we need to expose them with staticProvider4142app.use(express.static(__dirname + '/public'));4344app.set('views', __dirname + '/views');4546app.get('/', function(req, res){47 res.render('index.jade');48});4950app.listen(3000);51console.log('server listening on port 3000');
modified:lib/express.js+11 added-2 removed
58 unmodified lines
59 , Object.getOwnPropertyDescriptor(connect.middleware, key));60}6162/**63 * Backwards compat.63 * Error on createServer().64 */6566exports.createServer = createApplication;66exports.createServer = function(){67 console.warn('Warning: express.createServer() is deprecated, express');68 console.warn('applications no longer inherit from http.Server,');69 console.warn('please use:');70 console.warn('');71 console.warn(' var express = require("express");');72 console.warn(' var app = express();');73 console.warn('');74 return createApplication();75};7677/**78 * Expose the prototypes.79 */
16 unmodified lines