Skip to content
Myr

Merge branch 'master' of github.com:visionmedia/express

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 · parents 8e3cb61, 6c01e9a · Browse files · 11 files+279 added−204 removed
This commit

State

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

Diff stats

11 files+279 added−204 removed

  • tests:+77 added−43 removed
  • other:+202 added−161 removed

11 files+279 added−204 removed

modified:bin/express+39 added-39 removed
6 unmodified lines
7var express = require('../')8 , exec = require('child_process').exec9 , program = require('commander')10 , mkdirp = require('mkdirp')11 , os = require('os')12 , fs = require('fs');1314// CLI15
8 unmodified lines
24// Path2526var path = program.args.shift() || '.';2728// end-of-line code2930var eol = 'win32' == os.platform() ? '\r\n' : '\n'3132// Template engine3334program.template = 'jade';35if (program.ejs) program.template = 'ejs';
17 unmodified lines
53 , ''54 , 'exports.index = function(req, res){'55 , ' res.render(\'index\', { title: \'Express\' });'56 , '};'52].join('\r\n');57].join(eol);5859/**60 * Jade layout template.61 */
3 unmodified lines
65 , 'html'66 , ' head'67 , ' title= title'68 , ' link(rel=\'stylesheet\', href=\'/stylesheets/style.css\')'64 , ' body!= body'65].join('\r\n');69 , ' body'70 , ' block content'71].join(eol);7273/**74 * Jade index template.75 */7677var jadeIndex = [78 'extends layout'79 , ''80 , 'block content'72 'h1= title'73 , 'p Welcome to #{title}'74].join('\r\n');81 , ' h1= title'82 , ' p Welcome to #{title}'83].join(eol);8485/**77 * EJS layout template.86 * EJS index template.87 */8880var ejsLayout = [89var ejsIndex = [90 '<!DOCTYPE html>'91 , '<html>'92 , ' <head>'93 , ' <title><%= title %></title>'94 , ' <link rel=\'stylesheet\' href=\'/stylesheets/style.css\' />'95 , ' </head>'96 , ' <body>'88 , ' <%- body %>'97 , ' <h1><%= title %></h1>'98 , ' <p>Welcome to <%= title %></p>'99 , ' </body>'100 , '</html>'91].join('\r\n');101].join(eol);102103/**94 * EJS index template.95 */9697var ejsIndex = [98 '<h1><%= title %></h1>'99 , '<p>Welcome to <%= title %></p>'100 ].join('\r\n');101102/**104 * JSHTML layout template.105 */106107var jshtmlLayout = [
6 unmodified lines
114 , ' <body>'115 , ' @write(body)'116 , ' </body>'117 , '</html>'117].join('\r\n');118].join(eol);119120/**121 * JSHTML index template.122 */123124var jshtmlIndex = [125 '<h1>@write(title)</h1>'126 , '<p>Welcome to @write(title)</p>'126 ].join('\r\n');127 ].join(eol);128129/**130 * Default css template.131 */
6 unmodified lines
138 , ''139 , 'a {'140 , ' color: #00B7FF;'141 , '}'141].join('\r\n');142].join(eol);143144/**145 * Default less template.146 */
6 unmodified lines
153 , ''154 , 'a {'155 , ' color: #00B7FF;'156 , '}'156].join('\r\n');157].join(eol);158159/**160 * Default stylus template.161 */
3 unmodified lines
165 , ' padding: 50px'166 , ' font: 14px "Lucida Grande", Helvetica, Arial, sans-serif'167 , 'a'168 , ' color: #00B7FF'168].join('\r\n');169].join(eol);170171/**172 * App template.173 */
7 unmodified lines
181 , 'var express = require(\'express\')'182 , ' , routes = require(\'./routes\')'183 , ' , http = require(\'http\');'184 , ''184 , 'var app = module.exports = express();'185 , 'var app = express();'186 , ''187 , 'app.configure(function(){'188 , ' app.set(\'views\', __dirname + \'/views\');'189 , ' app.set(\'view engine\', \':TEMPLATE\');'
12 unmodified lines
202 , 'app.get(\'/\', routes.index);'203 , ''204 , 'http.createServer(app).listen(3000);'205 , ''205 , 'console.log("Express server listening for connections");'206 , 'console.log("Express server listening on port 3000");'207 , ''207].join('\r\n');208].join(eol);209210// Generate application211212(function createApplication(path) {
48 unmodified lines
261262 mkdir(path + '/views', function(){263 switch (program.template) {264 case 'ejs':264 write(path + '/views/layout.ejs', ejsLayout);265 write(path + '/views/index.ejs', ejsIndex);266 break;267 case 'jade':268 write(path + '/views/layout.jade', jadeLayout);
9 unmodified lines
278279 // CSS Engine support280 switch (program.css) {281 case 'stylus':282 app = app.replace('{css}', '\r\n app.use(require(\'stylus\').middleware({ src: __dirname + \'/public\' }));');282 app = app.replace('{css}', eol + ' app.use(require(\'stylus\').middleware({ src: __dirname + \'/public\' }));');283 break;284 default:285 app = app.replace('{css}', '');286 }287288 // Session support289 app = app.replace('{sess}', program.sessions290 ? '\n app.use(express.cookieParser(\'your secret here\'));\n app.use(express.session());'290 ? eol + ' app.use(express.cookieParser(\'your secret here\'));' + eol + ' app.use(express.session());'291 : '');292293 // Template support294 app = app.replace(':TEMPLATE', program.template);295296 // package.json297 var json = '{\n';298 json += ' "name": "application-name"\n';299 json += ' , "version": "0.0.1"\n';300 json += ' , "private": true\n';301 json += ' , "dependencies": {\n';302 json += ' "express": "' + express.version + '"\n';303 if (program.css) json += ' , "' + program.css + '": ">= 0.0.1"\n';304 if (program.template) json += ' , "' + program.template + '": ">= 0.0.1"\n';305 json += ' }\n';297 var json = '{' + eol;298 json += ' "name": "application-name"' + eol;299 json += ' , "version": "0.0.1"' + eol;300 json += ' , "private": true' + eol;301 json += ' , "dependencies": {' + eol;302 json += ' "express": "' + express.version + '"' + eol;303 if (program.css) json += ' , "' + program.css + '": ">= 0.0.1"' + eol;304 if (program.template) json += ' , "' + program.template + '": ">= 0.0.1"' + eol;305 json += ' }' + eol;306 json += '}';307308309 write(path + '/package.json', json);
55 unmodified lines
added:examples/cookie-sessions/index.js+47 added
deleted:examples/web-service/app.js-121 removed
12/**3 * Module dependencies.4 */56var express = require('../../lib/express');78var app = express.createServer();910// configuration1112// if we wanted to supply more than JSON, we could13// use something similar to the content-negotiation14// example.1516// here we validate the API key,17// by mounting this middleware to /api/v118// meaning only paths prefixed with "/api/v1"19// will cause this middleware to be invoked2021app.use('/api/v1', function(req, res, next){22 var key = req.query['api-key'];2324 // key isnt present25 if (!key) return next(new Error('api key required'));2627 // key is invalid28 if (!~apiKeys.indexOf(key)) return next(new Error('invalid api key'));2930 // all good, store req.key for route access31 req.key = key;32 next();33});3435// position our routes above the error handling middleware,36// and below our API middleware, since we want the API validation37// to take place BEFORE our routes38app.use(app.router);3940// middleware with an arity of 4 are considered41// error handling middleware. When you next(err)42// it will be passed through the defined middleware43// in order, but ONLY those with an arity of 4, ignoring44// regular middleware.45app.use(function(err, req, res, next){46 // whatever you want here, feel free to populate47 // properties on `err` to treat it differently in here,48 // or when you next(err) set res.statusCode= etc.49 res.send(500, { error: err.message });50});5152// our custom JSON 404 middleware. Since it's placed last53// it will be the last middleware called, if all others54// invoke next() and do not respond.55app.use(function(req, res){56 res.send(404, { error: "Lame, can't find that" });57});5859/**60 * Generate our unique identifier.61 */6263function uid() {64 return [65 Math.random() * 0xffff | 066 , Math.random() * 0xffff | 067 , Math.random() * 0xffff | 068 , Date.now()69 ].join('-');70}7172// map of valid api keys, typically mapped to73// account info with some sort of database like redis.74// api keys do _not_ serve as authentication, merely to75// track API usage or help prevent malicious behavior etc.7677var apiKeys = [uid(), uid(), uid()];7879console.log('valid keys:\n ', apiKeys.join('\n '));8081// these two objects will serve as our faux database8283var repos = [84 { name: 'express', url: 'http://github.com/visionmedia/express' }85 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }86 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }87];8889var users = [90 { name: 'tobi' }91 , { name: 'loki' }92 , { name: 'jane' }93];9495var userRepos = {96 tobi: [repos[0], repos[1]]97 , loki: [repos[1]]98 , jane: [repos[2]]99};100101// we now can assume the api key is valid,102// and simply expose the data103104app.get('/api/v1/users', function(req, res, next){105 res.send(users);106});107108app.get('/api/v1/repos', function(req, res, next){109 res.send(repos);110});111112app.get('/api/v1/user/:name/repos', function(req, res, next){113 var name = req.params.name114 , user = userRepos[name];115 116 if (user) res.send(user);117 else next();118});119120app.listen(3000);121console.log('Express server listening on port 3000');
added:examples/web-service/index.js+115 added
12/**3 * Module dependencies.4 */56var express = require('../../');78var app = module.exports = express();910// create an error with .status. we11// can then use the property in our12// custom error handler (Connect repects this prop as well)1314function error(status, msg) {15 var err = new Error(msg);16 err.status = status;17 return err;18}1920// if we wanted to supply more than JSON, we could21// use something similar to the content-negotiation22// example.2324// here we validate the API key,25// by mounting this middleware to /api26// meaning only paths prefixed with "/api"27// will cause this middleware to be invoked2829app.use('/api', function(req, res, next){30 var key = req.query['api-key'];3132 // key isnt present33 if (!key) return next(error(400, 'api key required'));3435 // key is invalid36 if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));3738 // all good, store req.key for route access39 req.key = key;40 next();41});4243// position our routes above the error handling middleware,44// and below our API middleware, since we want the API validation45// to take place BEFORE our routes46app.use(app.router);4748// middleware with an arity of 4 are considered49// error handling middleware. When you next(err)50// it will be passed through the defined middleware51// in order, but ONLY those with an arity of 4, ignoring52// regular middleware.53app.use(function(err, req, res, next){54 // whatever you want here, feel free to populate55 // properties on `err` to treat it differently in here.56 res.send(err.status || 500, { error: err.message });57});5859// our custom JSON 404 middleware. Since it's placed last60// it will be the last middleware called, if all others61// invoke next() and do not respond.62app.use(function(req, res){63 res.send(404, { error: "Lame, can't find that" });64});6566// map of valid api keys, typically mapped to67// account info with some sort of database like redis.68// api keys do _not_ serve as authentication, merely to69// track API usage or help prevent malicious behavior etc.7071var apiKeys = ['foo', 'bar', 'baz'];7273// these two objects will serve as our faux database7475var repos = [76 { name: 'express', url: 'http://github.com/visionmedia/express' }77 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }78 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }79];8081var users = [82 { name: 'tobi' }83 , { name: 'loki' }84 , { name: 'jane' }85];8687var userRepos = {88 tobi: [repos[0], repos[1]]89 , loki: [repos[1]]90 , jane: [repos[2]]91};9293// we now can assume the api key is valid,94// and simply expose the data9596app.get('/api/users', function(req, res, next){97 res.send(users);98});99100app.get('/api/repos', function(req, res, next){101 res.send(repos);102});103104app.get('/api/user/:name/repos', function(req, res, next){105 var name = req.params.name106 , user = userRepos[name];107 108 if (user) res.send(user);109 else next();110});111112if (!module.parent) {113 app.listen(3000);114 console.log('Express server listening on port 3000');115}
modified:lib/response.js+1 added-1 removed
410 unmodified lines
411res.cookie = function(name, val, options){412 options = options || {};413 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);414 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);415 if (undefined === options.path) options.path = '/';415 if (null == options.path) options.path = '/';416 var cookie = utils.serializeCookie(name, val, options);417 this.header('Set-Cookie', cookie);418 return this;419};
157 unmodified lines
modified:test/acceptance/ejs.js+3 added-3 removed
added:test/acceptance/web-service.js+48 added
modified:test/exports.js+3 added-3 removed
added:test/regression.js+22 added
modified:test/res.clearCookie.js-18 removed
modified:test/res.cookie.js+1 added-19 removed
36 unmodified lines
37 done();38 })39 })40 41 // it('should default path to "root"', function(done){42 // var app = express();43 // 44 // app.set('root', '/admin');45 // 46 // app.use(function(req, res){47 // res.cookie('name', 'tobi').end();48 // });49 // 50 // request(app)51 // .get('/')52 // .end(function(res){53 // var val = ['name=tobi; path=/admin'];54 // res.headers['set-cookie'].should.eql(val);55 // done();56 // })57 // })58 41 it('should allow multiple calls', function(done){42 var app = express();4344 app.use(function(req, res){
41 unmodified lines
8687 request(app)88 .get('/')89 .end(function(res){108 res.headers['set-cookie'][0].should.not.include.string('Thu, 01 Jan 1970 00:00:01 GMT');90 res.headers['set-cookie'][0].should.not.include('Thu, 01 Jan 1970 00:00:01 GMT');91 done();92 })93 })94 })
2 unmodified lines