Permalink
Browse files

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

  • Loading branch information...
2 parents 8e3cb61 + 6c01e9a commit 85ea5f67f498ca7fa43263abca7c3d68a1d518f9 @tj tj committed Dec 20, 2011
View
@@ -8,6 +8,7 @@ var express = require('../')
, exec = require('child_process').exec
, program = require('commander')
, mkdirp = require('mkdirp')
+ , os = require('os')
, fs = require('fs');
// CLI
@@ -24,6 +25,10 @@ program
var path = program.args.shift() || '.';
+// end-of-line code
+
+var eol = 'win32' == os.platform() ? '\r\n' : '\n'
+
// Template engine
program.template = 'jade';
@@ -49,7 +54,7 @@ var index = [
, 'exports.index = function(req, res){'
, ' res.render(\'index\', { title: \'Express\' });'
, '};'
-].join('\r\n');
+].join(eol);
/**
* Jade layout template.
@@ -61,43 +66,39 @@ var jadeLayout = [
, ' head'
, ' title= title'
, ' link(rel=\'stylesheet\', href=\'/stylesheets/style.css\')'
- , ' body!= body'
-].join('\r\n');
+ , ' body'
+ , ' block content'
+].join(eol);
/**
* Jade index template.
*/
var jadeIndex = [
- 'h1= title'
- , 'p Welcome to #{title}'
-].join('\r\n');
+ 'extends layout'
+ , ''
+ , 'block content'
+ , ' h1= title'
+ , ' p Welcome to #{title}'
+].join(eol);
/**
- * EJS layout template.
+ * EJS index template.
*/
-var ejsLayout = [
+var ejsIndex = [
'<!DOCTYPE html>'
, '<html>'
, ' <head>'
, ' <title><%= title %></title>'
, ' <link rel=\'stylesheet\' href=\'/stylesheets/style.css\' />'
, ' </head>'
, ' <body>'
- , ' <%- body %>'
+ , ' <h1><%= title %></h1>'
+ , ' <p>Welcome to <%= title %></p>'
, ' </body>'
, '</html>'
-].join('\r\n');
-
-/**
- * EJS index template.
- */
-
-var ejsIndex = [
- '<h1><%= title %></h1>'
- , '<p>Welcome to <%= title %></p>'
- ].join('\r\n');
+].join(eol);
/**
* JSHTML layout template.
@@ -114,7 +115,7 @@ var jshtmlLayout = [
, ' @write(body)'
, ' </body>'
, '</html>'
-].join('\r\n');
+].join(eol);
/**
* JSHTML index template.
@@ -123,7 +124,7 @@ var jshtmlLayout = [
var jshtmlIndex = [
'<h1>@write(title)</h1>'
, '<p>Welcome to @write(title)</p>'
- ].join('\r\n');
+ ].join(eol);
/**
* Default css template.
@@ -138,7 +139,7 @@ var css = [
, 'a {'
, ' color: #00B7FF;'
, '}'
-].join('\r\n');
+].join(eol);
/**
* Default less template.
@@ -153,7 +154,7 @@ var less = [
, 'a {'
, ' color: #00B7FF;'
, '}'
-].join('\r\n');
+].join(eol);
/**
* Default stylus template.
@@ -165,7 +166,7 @@ var stylus = [
, ' font: 14px "Lucida Grande", Helvetica, Arial, sans-serif'
, 'a'
, ' color: #00B7FF'
-].join('\r\n');
+].join(eol);
/**
* App template.
@@ -181,7 +182,7 @@ var app = [
, ' , routes = require(\'./routes\')'
, ' , http = require(\'http\');'
, ''
- , 'var app = module.exports = express();'
+ , 'var app = express();'
, ''
, 'app.configure(function(){'
, ' app.set(\'views\', __dirname + \'/views\');'
@@ -202,9 +203,9 @@ var app = [
, ''
, 'http.createServer(app).listen(3000);'
, ''
- , 'console.log("Express server listening for connections");'
+ , 'console.log("Express server listening on port 3000");'
, ''
-].join('\r\n');
+].join(eol);
// Generate application
@@ -261,7 +262,6 @@ function createApplicationAt(path) {
mkdir(path + '/views', function(){
switch (program.template) {
case 'ejs':
- write(path + '/views/layout.ejs', ejsLayout);
write(path + '/views/index.ejs', ejsIndex);
break;
case 'jade':
@@ -279,30 +279,30 @@ function createApplicationAt(path) {
// CSS Engine support
switch (program.css) {
case 'stylus':
- app = app.replace('{css}', '\r\n app.use(require(\'stylus\').middleware({ src: __dirname + \'/public\' }));');
+ app = app.replace('{css}', eol + ' app.use(require(\'stylus\').middleware({ src: __dirname + \'/public\' }));');
break;
default:
app = app.replace('{css}', '');
}
// Session support
app = app.replace('{sess}', program.sessions
- ? '\n app.use(express.cookieParser(\'your secret here\'));\n app.use(express.session());'
+ ? eol + ' app.use(express.cookieParser(\'your secret here\'));' + eol + ' app.use(express.session());'
: '');
// Template support
app = app.replace(':TEMPLATE', program.template);
// package.json
- var json = '{\n';
- json += ' "name": "application-name"\n';
- json += ' , "version": "0.0.1"\n';
- json += ' , "private": true\n';
- json += ' , "dependencies": {\n';
- json += ' "express": "' + express.version + '"\n';
- if (program.css) json += ' , "' + program.css + '": ">= 0.0.1"\n';
- if (program.template) json += ' , "' + program.template + '": ">= 0.0.1"\n';
- json += ' }\n';
+ var json = '{' + eol;
+ json += ' "name": "application-name"' + eol;
+ json += ' , "version": "0.0.1"' + eol;
+ json += ' , "private": true' + eol;
+ json += ' , "dependencies": {' + eol;
+ json += ' "express": "' + express.version + '"' + eol;
+ if (program.css) json += ' , "' + program.css + '": ">= 0.0.1"' + eol;
+ if (program.template) json += ' , "' + program.template + '": ">= 0.0.1"' + eol;
+ json += ' }' + eol;
json += '}';
@@ -0,0 +1,47 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('../../');
+
+var app = module.exports = express();
+
+/**
+ * Cookie session middleware using the given cookie `name`.
+ *
+ * Here we simply check for a signed cookie of the same name,
+ * then save the object as JSON on response, again as a signed cookie.
+ */
+
+function cookieSessions(name) {
+ return function(req, res, next) {
+ req.session = req.signedCookies[name] || {};
+
+ res.on('header', function(){
+ res.signedCookie(name, req.session);
+ });
+
+ next();
+ }
+}
+
+// for this we need cookie support! this will
+// populate req.{signedCookies,cookies}()
+
+app.use(express.cookieParser('manny is cool'));
+app.use(cookieSessions('sid'));
+app.use(count);
+
+// do something with our session
+
+function count(req, res) {
+ req.session.count = req.session.count || 0;
+ var n = req.session.count++;
+ res.send('viewed ' + n + ' times\n');
+}
+
+if (!module.parent) {
+ app.listen(3000);
+ console.log('Express server listening on port 3000');
+}
@@ -3,29 +3,37 @@
* Module dependencies.
*/
-var express = require('../../lib/express');
+var express = require('../../');
-var app = express.createServer();
+var app = module.exports = express();
-// configuration
+// create an error with .status. we
+// can then use the property in our
+// custom error handler (Connect repects this prop as well)
+
+function error(status, msg) {
+ var err = new Error(msg);
+ err.status = status;
+ return err;
+}
// if we wanted to supply more than JSON, we could
// use something similar to the content-negotiation
// example.
// here we validate the API key,
-// by mounting this middleware to /api/v1
-// meaning only paths prefixed with "/api/v1"
+// by mounting this middleware to /api
+// meaning only paths prefixed with "/api"
// will cause this middleware to be invoked
-app.use('/api/v1', function(req, res, next){
+app.use('/api', function(req, res, next){
var key = req.query['api-key'];
// key isnt present
- if (!key) return next(new Error('api key required'));
+ if (!key) return next(error(400, 'api key required'));
// key is invalid
- if (!~apiKeys.indexOf(key)) return next(new Error('invalid api key'));
+ if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));
// all good, store req.key for route access
req.key = key;
@@ -44,9 +52,8 @@ app.use(app.router);
// regular middleware.
app.use(function(err, req, res, next){
// whatever you want here, feel free to populate
- // properties on `err` to treat it differently in here,
- // or when you next(err) set res.statusCode= etc.
- res.send(500, { error: err.message });
+ // properties on `err` to treat it differently in here.
+ res.send(err.status || 500, { error: err.message });
});
// our custom JSON 404 middleware. Since it's placed last
@@ -56,27 +63,12 @@ app.use(function(req, res){
res.send(404, { error: "Lame, can't find that" });
});
-/**
- * Generate our unique identifier.
- */
-
-function uid() {
- return [
- Math.random() * 0xffff | 0
- , Math.random() * 0xffff | 0
- , Math.random() * 0xffff | 0
- , Date.now()
- ].join('-');
-}
-
// map of valid api keys, typically mapped to
// account info with some sort of database like redis.
// api keys do _not_ serve as authentication, merely to
// track API usage or help prevent malicious behavior etc.
-var apiKeys = [uid(), uid(), uid()];
-
-console.log('valid keys:\n ', apiKeys.join('\n '));
+var apiKeys = ['foo', 'bar', 'baz'];
// these two objects will serve as our faux database
@@ -101,21 +93,23 @@ var userRepos = {
// we now can assume the api key is valid,
// and simply expose the data
-app.get('/api/v1/users', function(req, res, next){
+app.get('/api/users', function(req, res, next){
res.send(users);
});
-app.get('/api/v1/repos', function(req, res, next){
+app.get('/api/repos', function(req, res, next){
res.send(repos);
});
-app.get('/api/v1/user/:name/repos', function(req, res, next){
+app.get('/api/user/:name/repos', function(req, res, next){
var name = req.params.name
, user = userRepos[name];
if (user) res.send(user);
else next();
});
-app.listen(3000);
-console.log('Express server listening on port 3000');
+if (!module.parent) {
+ app.listen(3000);
+ console.log('Express server listening on port 3000');
+}
View
@@ -412,7 +412,7 @@ res.cookie = function(name, val, options){
options = options || {};
if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);
- if (undefined === options.path) options.path = '/';
+ if (null == options.path) options.path = '/';
var cookie = utils.serializeCookie(name, val, options);
this.header('Set-Cookie', cookie);
return this;
View
@@ -10,9 +10,9 @@ describe('ejs', function(){
.end(function(res){
res.should.have.status(200);
res.should.have.header('Content-Type', 'text/html; charset=utf-8');
- res.body.should.include.string('<li>tobi tobi@learnboost.com</li>');
- res.body.should.include.string('<li>loki loki@learnboost.com</li>');
- res.body.should.include.string('<li>jane jane@learnboost.com</li>');
+ res.body.should.include('<li>tobi tobi@learnboost.com</li>');
+ res.body.should.include('<li>loki loki@learnboost.com</li>');
+ res.body.should.include('<li>jane jane@learnboost.com</li>');
done();
});
})
Oops, something went wrong.

0 comments on commit 85ea5f6

Please sign in to comment.