Skip to content
Myr

examples: make main app file names consistent

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

fixes #2408

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent dfa7ee4 · Browse files · 15 files+382 added−382 removed
This commit

State

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

Diff stats

15 files+382 added−382 removed

  • tests:+5 added−5 removed
  • other:+377 added−377 removed

15 files+382 added−382 removed

deleted:examples/auth/app.js-130 removed
1/**2 * Module dependencies.3 */45var express = require('../..');6var hash = require('./pass').hash;7var bodyParser = require('body-parser');8var session = require('express-session');910var app = module.exports = express();1112// config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');1617// middleware1819app.use(bodyParser.urlencoded({ extended: false }));20app.use(session({21 resave: false, // don't save session if unmodified22 saveUninitialized: false, // don't create session until something stored23 secret: 'shhhh, very secret'24}));2526// Session-persisted message middleware2728app.use(function(req, res, next){29 var err = req.session.error;30 var msg = req.session.success;31 delete req.session.error;32 delete req.session.success;33 res.locals.message = '';34 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';35 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';36 next();37});3839// dummy database4041var users = {42 tj: { name: 'tj' }43};4445// when you create a user, generate a salt46// and hash the password ('foobar' is the pass here)4748hash('foobar', function(err, salt, hash){49 if (err) throw err;50 // store the salt & hash in the "db"51 users.tj.salt = salt;52 users.tj.hash = hash;53});545556// Authenticate using our plain-object database of doom!5758function authenticate(name, pass, fn) {59 if (!module.parent) console.log('authenticating %s:%s', name, pass);60 var user = users[name];61 // query the db for the given username62 if (!user) return fn(new Error('cannot find user'));63 // apply the same algorithm to the POSTed password, applying64 // the hash against the pass / salt, if there is a match we65 // found the user66 hash(pass, user.salt, function(err, hash){67 if (err) return fn(err);68 if (hash == user.hash) return fn(null, user);69 fn(new Error('invalid password'));70 });71}7273function restrict(req, res, next) {74 if (req.session.user) {75 next();76 } else {77 req.session.error = 'Access denied!';78 res.redirect('/login');79 }80}8182app.get('/', function(req, res){83 res.redirect('/login');84});8586app.get('/restricted', restrict, function(req, res){87 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');88});8990app.get('/logout', function(req, res){91 // destroy the user's session to log them out92 // will be re-created next request93 req.session.destroy(function(){94 res.redirect('/');95 });96});9798app.get('/login', function(req, res){99 res.render('login');100});101102app.post('/login', function(req, res){103 authenticate(req.body.username, req.body.password, function(err, user){104 if (user) {105 // Regenerate session when signing in106 // to prevent fixation107 req.session.regenerate(function(){108 // Store the user's primary key109 // in the session store to be retrieved,110 // or in this case the entire user object111 req.session.user = user;112 req.session.success = 'Authenticated as ' + user.name113 + ' click to <a href="/logout">logout</a>. '114 + ' You may now access <a href="/restricted">/restricted</a>.';115 res.redirect('back');116 });117 } else {118 req.session.error = 'Authentication failed, please check your '119 + ' username and password.'120 + ' (use "tj" and "foobar")';121 res.redirect('/login');122 }123 });124});125126/* istanbul ignore next */127if (!module.parent) {128 app.listen(3000);129 console.log('Express started on port 3000');130}
added:examples/auth/index.js+130 added
1/**2 * Module dependencies.3 */45var express = require('../..');6var hash = require('./pass').hash;7var bodyParser = require('body-parser');8var session = require('express-session');910var app = module.exports = express();1112// config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');1617// middleware1819app.use(bodyParser.urlencoded({ extended: false }));20app.use(session({21 resave: false, // don't save session if unmodified22 saveUninitialized: false, // don't create session until something stored23 secret: 'shhhh, very secret'24}));2526// Session-persisted message middleware2728app.use(function(req, res, next){29 var err = req.session.error;30 var msg = req.session.success;31 delete req.session.error;32 delete req.session.success;33 res.locals.message = '';34 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';35 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';36 next();37});3839// dummy database4041var users = {42 tj: { name: 'tj' }43};4445// when you create a user, generate a salt46// and hash the password ('foobar' is the pass here)4748hash('foobar', function(err, salt, hash){49 if (err) throw err;50 // store the salt & hash in the "db"51 users.tj.salt = salt;52 users.tj.hash = hash;53});545556// Authenticate using our plain-object database of doom!5758function authenticate(name, pass, fn) {59 if (!module.parent) console.log('authenticating %s:%s', name, pass);60 var user = users[name];61 // query the db for the given username62 if (!user) return fn(new Error('cannot find user'));63 // apply the same algorithm to the POSTed password, applying64 // the hash against the pass / salt, if there is a match we65 // found the user66 hash(pass, user.salt, function(err, hash){67 if (err) return fn(err);68 if (hash == user.hash) return fn(null, user);69 fn(new Error('invalid password'));70 });71}7273function restrict(req, res, next) {74 if (req.session.user) {75 next();76 } else {77 req.session.error = 'Access denied!';78 res.redirect('/login');79 }80}8182app.get('/', function(req, res){83 res.redirect('/login');84});8586app.get('/restricted', restrict, function(req, res){87 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');88});8990app.get('/logout', function(req, res){91 // destroy the user's session to log them out92 // will be re-created next request93 req.session.destroy(function(){94 res.redirect('/');95 });96});9798app.get('/login', function(req, res){99 res.render('login');100});101102app.post('/login', function(req, res){103 authenticate(req.body.username, req.body.password, function(err, user){104 if (user) {105 // Regenerate session when signing in106 // to prevent fixation107 req.session.regenerate(function(){108 // Store the user's primary key109 // in the session store to be retrieved,110 // or in this case the entire user object111 req.session.user = user;112 req.session.success = 'Authenticated as ' + user.name113 + ' click to <a href="/logout">logout</a>. '114 + ' You may now access <a href="/restricted">/restricted</a>.';115 res.redirect('back');116 });117 } else {118 req.session.error = 'Authentication failed, please check your '119 + ' username and password.'120 + ' (use "tj" and "foobar")';121 res.redirect('/login');122 }123 });124});125126/* istanbul ignore next */127if (!module.parent) {128 app.listen(3000);129 console.log('Express started on port 3000');130}
deleted:examples/cookies/app.js-48 removed
added:examples/cookies/index.js+48 added
deleted:examples/downloads/app.js-36 removed
added:examples/downloads/index.js+36 added
deleted:examples/params/app.js-70 removed
added:examples/params/index.js+70 added
deleted:examples/resource/app.js-93 removed
1/**2 * Module dependencies.3 */45var express = require('../../');67var app = module.exports = express();89// Ad-hoc example resource method1011app.resource = function(path, obj) {12 this.get(path, obj.index);13 this.get(path + '/:a..:b.:format?', function(req, res){14 var a = parseInt(req.params.a, 10);15 var b = parseInt(req.params.b, 10);16 var format = req.params.format;17 obj.range(req, res, a, b, format);18 });19 this.get(path + '/:id', obj.show);20 this.delete(path + '/:id', function(req, res){21 var id = parseInt(req.params.id, 10);22 obj.destroy(req, res, id);23 });24};2526// Fake records2728var users = [29 { name: 'tj' }30 , { name: 'ciaran' }31 , { name: 'aaron' }32 , { name: 'guillermo' }33 , { name: 'simon' }34 , { name: 'tobi' }35];3637// Fake controller.3839var User = {40 index: function(req, res){41 res.send(users);42 },43 show: function(req, res){44 res.send(users[req.params.id] || { error: 'Cannot find user' });45 },46 destroy: function(req, res, id){47 var destroyed = id in users;48 delete users[id];49 res.send(destroyed ? 'destroyed' : 'Cannot find user');50 },51 range: function(req, res, a, b, format){52 var range = users.slice(a, b + 1);53 switch (format) {54 case 'json':55 res.send(range);56 break;57 case 'html':58 default:59 var html = '<ul>' + range.map(function(user){60 return '<li>' + user.name + '</li>';61 }).join('\n') + '</ul>';62 res.send(html);63 break;64 }65 }66};6768// curl http://localhost:3000/users -- responds with all users69// curl http://localhost:3000/users/1 -- responds with user 170// curl http://localhost:3000/users/4 -- responds with error71// curl http://localhost:3000/users/1..3 -- responds with several users72// curl -X DELETE http://localhost:3000/users/1 -- deletes the user7374app.resource('/users', User);7576app.get('/', function(req, res){77 res.send([78 '<h1>Examples:</h1> <ul>'79 , '<li>GET /users</li>'80 , '<li>GET /users/1</li>'81 , '<li>GET /users/3</li>'82 , '<li>GET /users/1..3</li>'83 , '<li>GET /users/1..3.json</li>'84 , '<li>DELETE /users/4</li>'85 , '</ul>'86 ].join('\n'));87});8889/* istanbul ignore next */90if (!module.parent) {91 app.listen(3000);92 console.log('Express started on port 3000');93}
added:examples/resource/index.js+93 added
1/**2 * Module dependencies.3 */45var express = require('../../');67var app = module.exports = express();89// Ad-hoc example resource method1011app.resource = function(path, obj) {12 this.get(path, obj.index);13 this.get(path + '/:a..:b.:format?', function(req, res){14 var a = parseInt(req.params.a, 10);15 var b = parseInt(req.params.b, 10);16 var format = req.params.format;17 obj.range(req, res, a, b, format);18 });19 this.get(path + '/:id', obj.show);20 this.delete(path + '/:id', function(req, res){21 var id = parseInt(req.params.id, 10);22 obj.destroy(req, res, id);23 });24};2526// Fake records2728var users = [29 { name: 'tj' }30 , { name: 'ciaran' }31 , { name: 'aaron' }32 , { name: 'guillermo' }33 , { name: 'simon' }34 , { name: 'tobi' }35];3637// Fake controller.3839var User = {40 index: function(req, res){41 res.send(users);42 },43 show: function(req, res){44 res.send(users[req.params.id] || { error: 'Cannot find user' });45 },46 destroy: function(req, res, id){47 var destroyed = id in users;48 delete users[id];49 res.send(destroyed ? 'destroyed' : 'Cannot find user');50 },51 range: function(req, res, a, b, format){52 var range = users.slice(a, b + 1);53 switch (format) {54 case 'json':55 res.send(range);56 break;57 case 'html':58 default:59 var html = '<ul>' + range.map(function(user){60 return '<li>' + user.name + '</li>';61 }).join('\n') + '</ul>';62 res.send(html);63 break;64 }65 }66};6768// curl http://localhost:3000/users -- responds with all users69// curl http://localhost:3000/users/1 -- responds with user 170// curl http://localhost:3000/users/4 -- responds with error71// curl http://localhost:3000/users/1..3 -- responds with several users72// curl -X DELETE http://localhost:3000/users/1 -- deletes the user7374app.resource('/users', User);7576app.get('/', function(req, res){77 res.send([78 '<h1>Examples:</h1> <ul>'79 , '<li>GET /users</li>'80 , '<li>GET /users/1</li>'81 , '<li>GET /users/3</li>'82 , '<li>GET /users/1..3</li>'83 , '<li>GET /users/1..3.json</li>'84 , '<li>DELETE /users/4</li>'85 , '</ul>'86 ].join('\n'));87});8889/* istanbul ignore next */90if (!module.parent) {91 app.listen(3000);92 console.log('Express started on port 3000');93}
modified:test/acceptance/auth.js+1 added-1 removed
1var app = require('../../examples/auth/app')1var app = require('../../examples/auth')2var request = require('supertest')34function getCookie(res) {5 return res.headers['set-cookie'][0].split(';')[0];
97 unmodified lines
modified:test/acceptance/cookies.js+1 added-1 removed
12var app = require('../../examples/cookies/app')2var app = require('../../examples/cookies')3 , request = require('supertest');45describe('cookies', function(){6 describe('GET /', function(){
70 unmodified lines
modified:test/acceptance/downloads.js+1 added-1 removed
12var app = require('../../examples/downloads/app')2var app = require('../../examples/downloads')3 , request = require('supertest');45describe('downloads', function(){6 describe('GET /', function(){
27 unmodified lines
modified:test/acceptance/params.js+1 added-1 removed
1var app = require('../../examples/params/app')1var app = require('../../examples/params')2var request = require('supertest')34describe('params', function(){5 describe('GET /', function(){
39 unmodified lines
modified:test/acceptance/resource.js+1 added-1 removed
1var app = require('../../examples/resource/app')1var app = require('../../examples/resource')2var request = require('supertest')34describe('resource', function(){5 describe('GET /', function(){
63 unmodified lines