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
Provenance
- Commit
42fd29e- Tree
305ca34- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
2d91eac
19 files changed+136 added−310 removed
modified:examples/jade/index.js+2 added-3 removed
7 unmodified lines8910111213121315141516171827 unmodified lines7 unmodified lines// Path to our public directory
var pub = __dirname + '/public';
// Auto-compile sass to css with "compiler"// and then serve with connect's staticProvider// setup middleware
var app = express.createServer();var app = express();app.use(app.router);app.use(express.static(pub));app.use(express.errorHandler());
27 unmodified linesdeleted: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
1-li.weapon= weapondeleted:examples/partials/views/ninja/index.jade-22 removed
1-h1= ninja.name2-3-// file, partial name, and partial object all match ('summary')4-// the partial filename prefix '_' is completely optional.5-6-// 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 })10-11-// 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
1-h2 Summary2-p= summary.email3-p= summary.description4-p taught by master #{summary.master}deleted:examples/partials/views/layout.jade-5 removed
1-!!!2-html3- head4- title Partials Example5- body!= bodydeleted:examples/partials/views/li.jade-1 removed
1-li= valuedeleted:examples/partials/app.js-32 removed
1234567891011121314151617181920212223242526272829303132
/** * Module dependencies. */
var express = require('../../lib/express');
var app = express.createServer();
// Optional since express defaults to CWD/views
app.set('views', __dirname + '/views');
// Set our default template engine to "jade"// which prevents the need for extensions// (although you can still mix and match)app.set('view engine', 'jade');
// Dummy recordvar ninja = { name: 'leonardo', summary: { email: 'hunter.loftis+github@gmail.com', master: 'splinter', description: 'peaceful leader' }, weapons: ['katana', 'fists', 'shell'], victims: ['shredder', 'brain', 'beebop', 'rocksteady']};
app.get('/', function(req, res){ res.render('ninja', { ninja: ninja });});
app.listen(3000);console.log('Express app started on port 3000');deleted:examples/route-middleware/app.js-85 removed
1-/**2- * Module dependencies.3- */4-5-var express = require('../../lib/express');6-7-var app = express.createServer();8-9-// 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)15-16-// Dummy users17-var 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-];22-23-function 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-}33-34-function 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-}47-48-function 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-}57-58-// Middleware for faux authentication59-// you would of course implement something real,60-// but this illustrates how an authenticated user61-// may interact with middleware62-63-app.use(function(req, res, next){64- req.authenticatedUser = users[0];65- next();66-});67-68-app.get('/', function(req, res){69- res.redirect('/user/0');70-});71-72-app.get('/user/:id', loadUser, function(req, res){73- res.send('Viewing user ' + req.user.name);74-});75-76-app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){77- res.send('Editing user ' + req.user.name);78-});79-80-app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){81- res.send('Deleted user ' + req.user.name);82-});83-84-app.listen(3000);85-console.log('Express app started on port 3000');added:examples/route-middleware/index.js+85 added
1+/**2+ * Module dependencies.3+ */4+5+var express = require('../../lib/express');6+7+var app = express();8+9+// 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)15+16+// Dummy users17+var 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+];22+23+function 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+}33+34+function 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+}47+48+function 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+}57+58+// Middleware for faux authentication59+// you would of course implement something real,60+// but this illustrates how an authenticated user61+// may interact with middleware62+63+app.use(function(req, res, next){64+ req.authenticatedUser = users[0];65+ next();66+});67+68+app.get('/', function(req, res){69+ res.redirect('/user/0');70+});71+72+app.get('/user/:id', loadUser, function(req, res){73+ res.send('Viewing user ' + req.user.name);74+});75+76+app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){77+ res.send('Editing user ' + req.user.name);78+});79+80+app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){81+ res.send('Deleted user ' + req.user.name);82+});83+84+app.listen(3000);85+console.log('Express app started on port 3000');deleted:examples/route-separation/app.js-38 removed
1-2-/**3- * Module dependencies.4- */5-6-var express = require('../../lib/express')7- , app = express.createServer()8- , site = require('./site')9- , post = require('./post')10- , user = require('./user');11-12-// Config13-14-app.set('view engine', 'ejs');15-app.set('views', __dirname + '/views');16-app.use(express.cookieParser());17-app.use(express.methodOverride());18-app.use(express.static(__dirname + '/public'));19-20-// General21-22-app.get('/', site.index);23-24-// User25-26-app.all('/users', user.list);27-app.all('/user/:id/:op?', user.load);28-app.get('/user/:id', user.view);29-app.get('/user/:id/view', user.view);30-app.get('/user/:id/edit', user.edit);31-app.put('/user/:id/edit', user.update);32-33-// Posts34-35-app.get('/posts', post.list);36-37-app.listen(3000);38-console.log('Express app started on port 3000');added:examples/route-separation/index.js+38 added
1+2+/**3+ * Module dependencies.4+ */5+6+var express = require('../..')7+ , app = express()8+ , site = require('./site')9+ , post = require('./post')10+ , user = require('./user');11+12+// Config13+14+app.set('view engine', 'ejs');15+app.set('views', __dirname + '/views');16+app.use(express.cookieParser());17+app.use(express.methodOverride());18+app.use(express.static(__dirname + '/public'));19+20+// General21+22+app.get('/', site.index);23+24+// User25+26+app.all('/users', user.list);27+app.all('/user/:id/:op?', user.load);28+app.get('/user/:id', user.view);29+app.get('/user/:id/view', user.view);30+app.get('/user/:id/edit', user.edit);31+app.put('/user/:id/edit', user.update);32+33+// Posts34+35+app.get('/posts', post.list);36+37+app.listen(3000);38+console.log('Express app started on port 3000');deleted:examples/say/app.js-44 removed
1-2-/**3- * Module dependencies.4- */5-6-var express = require('../../')7- , path = require('path')8- , exec = require('child_process').exec9- , fs = require('fs');10-11-/**12- * Error handler.13- */14-15-function 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(/\./, ' ');24-25- exec('say -v "' + voice + '" '26- + err.message27- + ' on line ' + lineno28- + ' of ' + basename + '.'29- + ' The contents of this line is '30- + ' "' + line + '".');31-32- res.send(500);33- }34-}35-36-var app = express.createServer();37-38-app.get('/', function(request, response){39- if (request.is(foo)) response.end('bar');40-});41-42-app.use(errorHandler('Vicki'));43-44-app.listen(3000);deleted:examples/stylus/public/.gitignore-1 removed
1-*.cssdeleted:examples/stylus/views/index.jade-2 removed
1-h1 Stylus2-p Just an example of using Stylus with Express.deleted:examples/stylus/views/layout.jade-6 removed
1-html2- head3- title Stylus Example4- link(rel='stylesheet', href='/reset.css')5- link(rel='stylesheet', href='/main.css')6- body!= bodydeleted:examples/stylus/views/main.styl-8 removed
1-2-body3- font 14px helvetica, arial, sans-serif4- padding 50px5- h16- font-size 50px7- p8- margin 15px 0deleted:examples/stylus/app.js-51 removed
1-2-/**3- * Module dependencies.4- */5-6-var express = require('../../lib/express')7- , stylus = require('stylus');8-9-var app = express.createServer();10-11-// $ npm install stylus12-13-// completely optional, however14-// the compile function allows you to15-// define additional functions exposed to Stylus,16-// alter settings, etc17-18-function compile(str, path) {19- return stylus(str)20- .set('filename', path)21- .set('compress', true);22-};23-24-// add the stylus middleware, which re-compiles when25-// a stylesheet has changed, compiling FROM src,26-// TO dest. dest is optional, defaulting to src27-28-app.use(stylus.middleware({29- src: __dirname + '/views'30- , dest: __dirname + '/public'31- , compile: compile32-}));33-34-35-// minimal setup both reading and writting to ./public36-// would look like:37-// app.use(stylus.middleware({ src: __dirname + '/public' }));38-39-// the middleware itself does not serve the static40-// css files, so we need to expose them with staticProvider41-42-app.use(express.static(__dirname + '/public'));43-44-app.set('views', __dirname + '/views');45-46-app.get('/', function(req, res){47- res.render('index.jade');48-});49-50-app.listen(3000);51-console.log('server listening on port 3000');modified:lib/express.js+11 added-2 removed
58 unmodified lines59 , Object.getOwnPropertyDescriptor(connect.middleware, key));60 }61 62 /**63- * Backwards compat.63+ * Error on createServer().64 */65 66-exports.createServer = createApplication;66+exports.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+};76 77 /**78 * Expose the prototypes.79 */16 unmodified lines