Skip to content
This repository
Browse code

refactor vhost example

  • Loading branch information...
commit bb29da5980df289ceccb3b18cff16368c7f54452 1 parent 480d006
TJ Holowaychuk authored January 13, 2013
37  examples/vhost/index.js
@@ -5,28 +5,26 @@
5 5
 
6 6
 var express = require('../..');
7 7
 
8  
-// Edit /etc/vhosts
  8
+/*
  9
+edit /etc/vhosts:
9 10
 
10  
-// First app
  11
+127.0.0.1       foo.example.com
  12
+127.0.0.1       bar.example.com
  13
+127.0.0.1       example.com
  14
+*/
11 15
 
12  
-var one = express();
  16
+// Main app
13 17
 
14  
-one.use(express.logger());
  18
+var main = express();
15 19
 
16  
-one.get('/', function(req, res){
17  
-  res.send('Hello from app one!')
18  
-});
  20
+main.use(express.logger('dev'));
19 21
 
20  
-one.get('/:sub', function(req, res){
21  
-  res.send('requsted ' + req.params.sub);
  22
+main.get('/', function(req, res){
  23
+  res.send('Hello from main app!')
22 24
 });
23 25
 
24  
-// App two
25  
-
26  
-var two = express();
27  
-
28  
-two.get('/', function(req, res){
29  
-  res.send('Hello from app two!')
  26
+main.get('/:sub', function(req, res){
  27
+  res.send('requsted ' + req.params.sub);
30 28
 });
31 29
 
32 30
 // Redirect app
@@ -35,16 +33,15 @@ var redirect = express();
35 33
 
36 34
 redirect.all('*', function(req, res){
37 35
   console.log(req.subdomains);
38  
-  res.redirect('http://localhost:3000/' + req.subdomains[0]);
  36
+  res.redirect('http://example.com:3000/' + req.subdomains[0]);
39 37
 });
40 38
 
41 39
 // Main app
42 40
 
43 41
 var app = express();
44 42
 
45  
-app.use(express.vhost('*.localhost', redirect))
46  
-app.use(express.vhost('localhost', one));
47  
-app.use(express.vhost('dev', two));
  43
+app.use(express.vhost('*.example.com', redirect))
  44
+app.use(express.vhost('example.com', main));
48 45
 
49 46
 app.listen(3000);
50  
-console.log('Express app started on port 3000');
  47
+console.log('Express app started on port 3000');
22  lib/response.js
@@ -187,7 +187,7 @@ res.json = function(obj){
187 187
   // content-type
188 188
   this.charset = this.charset || 'utf-8';
189 189
   this.get('Content-Type') || this.set('Content-Type', 'application/json');
190  
-  
  190
+
191 191
   return this.send(body);
192 192
 };
193 193
 
@@ -231,7 +231,7 @@ res.jsonp = function(obj){
231 231
   // content-type
232 232
   this.charset = this.charset || 'utf-8';
233 233
   this.set('Content-Type', 'application/json');
234  
-  
  234
+
235 235
   // jsonp
236 236
   if (callback) {
237 237
     this.set('Content-Type', 'text/javascript');
@@ -244,7 +244,7 @@ res.jsonp = function(obj){
244 244
 
245 245
 /**
246 246
  * Transfer the file at the given `path`.
247  
- * 
  247
+ *
248 248
  * Automatically sets the _Content-Type_ response header field.
249 249
  * The callback `fn(err)` is invoked when the transfer is complete
250 250
  * or when an error occurs. Be sure to check `res.sentHeader`
@@ -266,7 +266,7 @@ res.jsonp = function(obj){
266 266
  *     app.get('/user/:uid/photos/:file', function(req, res){
267 267
  *       var uid = req.params.uid
268 268
  *         , file = req.params.file;
269  
- *     
  269
+ *
270 270
  *       req.user.mayViewFilesFrom(uid, function(yes){
271 271
  *         if (yes) {
272 272
  *           res.sendfile('/uploads/' + uid + '/' + file);
@@ -411,11 +411,11 @@ res.type = function(type){
411 411
  *      'text/plain': function(){
412 412
  *        res.send('hey');
413 413
  *      },
414  
- *    
  414
+ *
415 415
  *      'text/html': function(){
416 416
  *        res.send('<p>hey</p>');
417 417
  *      },
418  
- *    
  418
+ *
419 419
  *      'appliation/json': function(){
420 420
  *        res.send({ message: 'hey' });
421 421
  *      }
@@ -428,11 +428,11 @@ res.type = function(type){
428 428
  *      text: function(){
429 429
  *        res.send('hey');
430 430
  *      },
431  
- *    
  431
+ *
432 432
  *      html: function(){
433 433
  *        res.send('<p>hey</p>');
434 434
  *      },
435  
- *    
  435
+ *
436 436
  *      json: function(){
437 437
  *        res.send({ message: 'hey' });
438 438
  *      }
@@ -501,7 +501,7 @@ res.attachment = function(filename){
501 501
  *    res.set('Accept', 'application/json');
502 502
  *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
503 503
  *
504  
- * Aliased as `res.header()`. 
  504
+ * Aliased as `res.header()`.
505 505
  *
506 506
  * @param {String|Object} field
507 507
  * @param {String} val
@@ -509,7 +509,7 @@ res.attachment = function(filename){
509 509
  * @api public
510 510
  */
511 511
 
512  
-res.set = 
  512
+res.set =
513 513
 res.header = function(field, val){
514 514
   if (2 == arguments.length) {
515 515
     this.setHeader(field, '' + val);
@@ -607,7 +607,7 @@ res.cookie = function(name, val, options){
607 607
  * Mounting:
608 608
  *
609 609
  *   When an application is mounted, and `res.redirect()`
610  
- *   is given a path that does _not_ lead with "/". For 
  610
+ *   is given a path that does _not_ lead with "/". For
611 611
  *   example suppose a "blog" app is mounted at "/blog",
612 612
  *   the following redirect would result in "/blog/login":
613 613
  *
14  test/res.sendfile.js
@@ -127,7 +127,7 @@ describe('res', function(){
127 127
         });
128 128
       })
129 129
     })
130  
-    
  130
+
131 131
     describe('with a relative path', function(){
132 132
       it('should transfer the file', function(done){
133 133
         var app = express();
@@ -144,7 +144,7 @@ describe('res', function(){
144 144
           done();
145 145
         });
146 146
       })
147  
-      
  147
+
148 148
       it('should serve relative to "root"', function(done){
149 149
         var app = express();
150 150
 
@@ -160,7 +160,7 @@ describe('res', function(){
160 160
           done();
161 161
         });
162 162
       })
163  
-      
  163
+
164 164
       it('should consider ../ malicious when "root" is not set', function(done){
165 165
         var app = express();
166 166
 
@@ -172,7 +172,7 @@ describe('res', function(){
172 172
         .get('/')
173 173
         .expect(403, done);
174 174
       })
175  
-      
  175
+
176 176
       it('should allow ../ when "root" is set', function(done){
177 177
         var app = express();
178 178
 
@@ -184,7 +184,7 @@ describe('res', function(){
184 184
         .get('/')
185 185
         .expect(200, done);
186 186
       })
187  
-      
  187
+
188 188
       it('should disallow requesting out of "root"', function(done){
189 189
         var app = express();
190 190
 
@@ -196,7 +196,7 @@ describe('res', function(){
196 196
         .get('/')
197 197
         .expect(403, done);
198 198
       })
199  
-      
  199
+
200 200
       it('should next(404) when not found', function(done){
201 201
         var app = express()
202 202
           , calls = 0;
@@ -208,7 +208,7 @@ describe('res', function(){
208 208
         app.use(function(req, res){
209 209
           assert(0, 'this should not be called');
210 210
         });
211  
-        
  211
+
212 212
         app.use(function(err, req, res, next){
213 213
           ++calls;
214 214
           next(err);

0 notes on commit bb29da5

Please sign in to comment.
Something went wrong with that request. Please try again.