refactor vhost example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by Julian Gruber · parent a4d7b75 · Browse files · 3 files·+34 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−37 removed
- tests:+7 added−7 removed
- other:+27 added−30 removed
Provenance
- Commit
ab75fa0- Tree
f1bc855- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
Julian Gruber <julian@juliangruber.com>- Parent
a4d7b75
3 files changed+34 added−37 removed
modified:examples/vhost/index.js+17 added-20 removed
3 unmodified lines45678891010111213141512161714181920211617222324252026272829242526272829303130313233343538363738394041424546474344454650473 unmodified lines */
var express = require('../..');
// Edit /etc/vhosts/*edit /etc/vhosts:
// First app127.0.0.1 foo.example.com127.0.0.1 bar.example.com127.0.0.1 example.com*/
var one = express();// Main app
one.use(express.logger());var main = express();
main.use(express.logger('dev'));
one.get('/', function(req, res){ res.send('Hello from app one!')main.get('/', function(req, res){ res.send('Hello from main app!')});
one.get('/:sub', function(req, res){main.get('/:sub', function(req, res){ res.send('requsted ' + req.params.sub);});
// App two
var two = express();
two.get('/', function(req, res){ res.send('Hello from app two!')});
// Redirect app
var redirect = express();
redirect.all('*', function(req, res){ console.log(req.subdomains); res.redirect('http://localhost:3000/' + req.subdomains[0]); res.redirect('http://example.com:3000/' + req.subdomains[0]);});
// Main app
var app = express();
app.use(express.vhost('*.localhost', redirect))app.use(express.vhost('localhost', one));app.use(express.vhost('dev', two));app.use(express.vhost('*.example.com', redirect))app.use(express.vhost('example.com', main));
app.listen(3000);console.log('Express app started on port 3000');No newline at end of fileconsole.log('Express app started on port 3000');modified:lib/response.js+10 added-10 removed
185 unmodified lines186 187 // content-type188 this.charset = this.charset || 'utf-8';189 this.get('Content-Type') || this.set('Content-Type', 'application/json');190- 190+191 return this.send(body);192 };193 194 /**35 unmodified lines230 231 // content-type232 this.charset = this.charset || 'utf-8';233 this.set('Content-Type', 'application/json');234- 234+235 // jsonp236 if (callback) {237 this.set('Content-Type', 'text/javascript');238 var cb = callback.replace(/[^\[\]\w$.]/g, '');4 unmodified lines243 };244 245 /**246 * Transfer the file at the given `path`.247- * 247+ *248 * Automatically sets the _Content-Type_ response header field.249 * The callback `fn(err)` is invoked when the transfer is complete250 * or when an error occurs. Be sure to check `res.sentHeader`251 * if you wish to attempt responding, as the header and some data13 unmodified lines265 *266 * app.get('/user/:uid/photos/:file', function(req, res){267 * var uid = req.params.uid268 * , file = req.params.file;269- * 269+ *270 * req.user.mayViewFilesFrom(uid, function(yes){271 * if (yes) {272 * res.sendfile('/uploads/' + uid + '/' + file);273 * } else {136 unmodified lines410 * res.format({411 * 'text/plain': function(){412 * res.send('hey');413 * },414- * 414+ *415 * 'text/html': function(){416 * res.send('<p>hey</p>');417 * },418- * 418+ *419 * 'appliation/json': function(){420 * res.send({ message: 'hey' });421 * }422 * });4 unmodified lines427 * res.format({428 * text: function(){429 * res.send('hey');430 * },431- * 431+ *432 * html: function(){433 * res.send('<p>hey</p>');434 * },435- * 435+ *436 * json: function(){437 * res.send({ message: 'hey' });438 * }439 * });60 unmodified lines500 *501 * res.set('Accept', 'application/json');502 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });503 *504- * Aliased as `res.header()`. 504+ * Aliased as `res.header()`.505 *506 * @param {String|Object} field507 * @param {String} val508 * @return {ServerResponse} for chaining509 * @api public510 */511 512-res.set = 512+res.set =513 res.header = function(field, val){514 if (2 == arguments.length) {515 this.setHeader(field, '' + val);516 } else {236 unmodified linesmodified:test/res.sendfile.js+7 added-7 removed
125 unmodified lines126 done();127 });128 })129 })130- 130+131 describe('with a relative path', function(){132 it('should transfer the file', function(done){133 var app = express();134 8 unmodified lines143 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');144 done();145 });146 })147- 147+148 it('should serve relative to "root"', function(done){149 var app = express();150 151 app.use(function(req, res){7 unmodified lines159 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');160 done();161 });162 })163- 163+164 it('should consider ../ malicious when "root" is not set', function(done){165 var app = express();166 167 app.use(function(req, res){3 unmodified lines171 request(app)172 .get('/')173 .expect(403, done);174 })175- 175+176 it('should allow ../ when "root" is set', function(done){177 var app = express();178 179 app.use(function(req, res){3 unmodified lines183 request(app)184 .get('/')185 .expect(200, done);186 })187- 187+188 it('should disallow requesting out of "root"', function(done){189 var app = express();190 191 app.use(function(req, res){3 unmodified lines195 request(app)196 .get('/')197 .expect(403, done);198 })199- 199+200 it('should next(404) when not found', function(done){201 var app = express()202 , calls = 0;203 3 unmodified lines207 208 app.use(function(req, res){209 assert(0, 'this should not be called');210 });211- 211+212 app.use(function(err, req, res, next){213 ++calls;214 next(err);215 });27 unmodified lines