Skip to content
Myr

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

3 files+34 added−37 removed

modified:examples/vhost/index.js+17 added-20 removed
modified:lib/response.js+10 added-10 removed
185 unmodified lines
186187 // content-type188 this.charset = this.charset || 'utf-8';189 this.get('Content-Type') || this.set('Content-Type', 'application/json');190 190191 return this.send(body);192};193194/**
35 unmodified lines
230231 // content-type232 this.charset = this.charset || 'utf-8';233 this.set('Content-Type', 'application/json');234 234235 // jsonp236 if (callback) {237 this.set('Content-Type', 'text/javascript');238 var cb = callback.replace(/[^\[\]\w$.]/g, '');
4 unmodified lines
243};244245/**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 data
13 unmodified lines
265 *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 lines
410 * 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 lines
427 * 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 lines
500 *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 */511512res.set = 512res.set =513res.header = function(field, val){514 if (2 == arguments.length) {515 this.setHeader(field, '' + val);516 } else {
236 unmodified lines
modified:test/res.sendfile.js+7 added-7 removed
125 unmodified lines
126 done();127 });128 })129 })130 130131 describe('with a relative path', function(){132 it('should transfer the file', function(done){133 var app = express();134
8 unmodified lines
143 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');144 done();145 });146 })147 147148 it('should serve relative to "root"', function(done){149 var app = express();150151 app.use(function(req, res){
7 unmodified lines
159 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');160 done();161 });162 })163 163164 it('should consider ../ malicious when "root" is not set', function(done){165 var app = express();166167 app.use(function(req, res){
3 unmodified lines
171 request(app)172 .get('/')173 .expect(403, done);174 })175 175176 it('should allow ../ when "root" is set', function(done){177 var app = express();178179 app.use(function(req, res){
3 unmodified lines
183 request(app)184 .get('/')185 .expect(200, done);186 })187 187188 it('should disallow requesting out of "root"', function(done){189 var app = express();190191 app.use(function(req, res){
3 unmodified lines
195 request(app)196 .get('/')197 .expect(403, done);198 })199 199200 it('should next(404) when not found', function(done){201 var app = express()202 , calls = 0;203
3 unmodified lines
207208 app.use(function(req, res){209 assert(0, 'this should not be called');210 });211 211212 app.use(function(err, req, res, next){213 ++calls;214 next(err);215 });
27 unmodified lines