Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor vhost example

  • Loading branch information...
commit bb29da5980df289ceccb3b18cff16368c7f54452 1 parent 480d006
@tj tj authored
Showing with 35 additions and 38 deletions.
  1. +17 −20 examples/vhost/index.js
  2. +11 −11 lib/response.js
  3. +7 −7 test/res.sendfile.js
View
37 examples/vhost/index.js
@@ -5,28 +5,26 @@
var express = require('../..');
-// Edit /etc/vhosts
+/*
+edit /etc/vhosts:
-// First app
+127.0.0.1 foo.example.com
+127.0.0.1 bar.example.com
+127.0.0.1 example.com
+*/
-var one = express();
+// Main app
-one.use(express.logger());
+var main = express();
-one.get('/', function(req, res){
- res.send('Hello from app one!')
-});
+main.use(express.logger('dev'));
-one.get('/:sub', function(req, res){
- res.send('requsted ' + req.params.sub);
+main.get('/', function(req, res){
+ res.send('Hello from main app!')
});
-// App two
-
-var two = express();
-
-two.get('/', function(req, res){
- res.send('Hello from app two!')
+main.get('/:sub', function(req, res){
+ res.send('requsted ' + req.params.sub);
});
// Redirect app
@@ -35,16 +33,15 @@ 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');
+console.log('Express app started on port 3000');
View
22 lib/response.js
@@ -187,7 +187,7 @@ res.json = function(obj){
// content-type
this.charset = this.charset || 'utf-8';
this.get('Content-Type') || this.set('Content-Type', 'application/json');
-
+
return this.send(body);
};
@@ -231,7 +231,7 @@ res.jsonp = function(obj){
// content-type
this.charset = this.charset || 'utf-8';
this.set('Content-Type', 'application/json');
-
+
// jsonp
if (callback) {
this.set('Content-Type', 'text/javascript');
@@ -244,7 +244,7 @@ res.jsonp = function(obj){
/**
* Transfer the file at the given `path`.
- *
+ *
* Automatically sets the _Content-Type_ response header field.
* The callback `fn(err)` is invoked when the transfer is complete
* or when an error occurs. Be sure to check `res.sentHeader`
@@ -266,7 +266,7 @@ res.jsonp = function(obj){
* app.get('/user/:uid/photos/:file', function(req, res){
* var uid = req.params.uid
* , file = req.params.file;
- *
+ *
* req.user.mayViewFilesFrom(uid, function(yes){
* if (yes) {
* res.sendfile('/uploads/' + uid + '/' + file);
@@ -411,11 +411,11 @@ res.type = function(type){
* 'text/plain': function(){
* res.send('hey');
* },
- *
+ *
* 'text/html': function(){
* res.send('<p>hey</p>');
* },
- *
+ *
* 'appliation/json': function(){
* res.send({ message: 'hey' });
* }
@@ -428,11 +428,11 @@ res.type = function(type){
* text: function(){
* res.send('hey');
* },
- *
+ *
* html: function(){
* res.send('<p>hey</p>');
* },
- *
+ *
* json: function(){
* res.send({ message: 'hey' });
* }
@@ -501,7 +501,7 @@ res.attachment = function(filename){
* res.set('Accept', 'application/json');
* res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
*
- * Aliased as `res.header()`.
+ * Aliased as `res.header()`.
*
* @param {String|Object} field
* @param {String} val
@@ -509,7 +509,7 @@ res.attachment = function(filename){
* @api public
*/
-res.set =
+res.set =
res.header = function(field, val){
if (2 == arguments.length) {
this.setHeader(field, '' + val);
@@ -607,7 +607,7 @@ res.cookie = function(name, val, options){
* Mounting:
*
* When an application is mounted, and `res.redirect()`
- * is given a path that does _not_ lead with "/". For
+ * is given a path that does _not_ lead with "/". For
* example suppose a "blog" app is mounted at "/blog",
* the following redirect would result in "/blog/login":
*
View
14 test/res.sendfile.js
@@ -127,7 +127,7 @@ describe('res', function(){
});
})
})
-
+
describe('with a relative path', function(){
it('should transfer the file', function(done){
var app = express();
@@ -144,7 +144,7 @@ describe('res', function(){
done();
});
})
-
+
it('should serve relative to "root"', function(done){
var app = express();
@@ -160,7 +160,7 @@ describe('res', function(){
done();
});
})
-
+
it('should consider ../ malicious when "root" is not set', function(done){
var app = express();
@@ -172,7 +172,7 @@ describe('res', function(){
.get('/')
.expect(403, done);
})
-
+
it('should allow ../ when "root" is set', function(done){
var app = express();
@@ -184,7 +184,7 @@ describe('res', function(){
.get('/')
.expect(200, done);
})
-
+
it('should disallow requesting out of "root"', function(done){
var app = express();
@@ -196,7 +196,7 @@ describe('res', function(){
.get('/')
.expect(403, done);
})
-
+
it('should next(404) when not found', function(done){
var app = express()
, calls = 0;
@@ -208,7 +208,7 @@ describe('res', function(){
app.use(function(req, res){
assert(0, 'this should not be called');
});
-
+
app.use(function(err, req, res, next){
++calls;
next(err);
Please sign in to comment.
Something went wrong with that request. Please try again.