Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

issues #997 code review #1160

Closed
wants to merge 1 commit into from

3 participants

@munichlinux

I added a method to req object which will help to build a absolute uri.

@tj tj commented on the diff
lib/request.js
@@ -132,6 +132,19 @@ req.acceptsLanguage = function(lang){
};
/**
+ * Builds absolute uri from req,
+ * Example: http://localhost/foo
+ *
+ * @param {String} host
+ * @return {String}
+ * @api public
+ */
+
+req.buildAbsoluteUri = function(host) {
+ return this.protocol + '://' + (host || this.get('host')) + this.path;
@tj
tj added a note

it's a bit tricky because there are a few things I would want to include ideally. I think the most common use would probably be adding a pathname and query-string via object or string, something like req.buildUrl('/foo', { sort: 'asc' }), but even then im not sure it belongs in Express unless we can come up with a better API for a few reasons. one) it looks weird, two) it would require that people do stuff like res.redirect(req.buildUrl('/path', req.query)) which kinda sucks if they want to retain a query-string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jonathanong

can no longer merge. we can move this to the discussion #1377

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 0 deletions.
  1. +13 −0 lib/request.js
  2. +33 −0 test/req.absoluteuri.js
View
13 lib/request.js
@@ -132,6 +132,19 @@ req.acceptsLanguage = function(lang){
};
/**
+ * Builds absolute uri from req,
+ * Example: http://localhost/foo
+ *
+ * @param {String} host
+ * @return {String}
+ * @api public
+ */
+
+req.buildAbsoluteUri = function(host) {
+ return this.protocol + '://' + (host || this.get('host')) + this.path;
@tj
tj added a note

it's a bit tricky because there are a few things I would want to include ideally. I think the most common use would probably be adding a pathname and query-string via object or string, something like req.buildUrl('/foo', { sort: 'asc' }), but even then im not sure it belongs in Express unless we can come up with a better API for a few reasons. one) it looks weird, two) it would require that people do stuff like res.redirect(req.buildUrl('/path', req.query)) which kinda sucks if they want to retain a query-string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+}
+
+/**
* Return an array of Accepted media types
* ordered from highest quality to lowest.
*
View
33 test/req.absoluteuri.js
@@ -0,0 +1,33 @@
+var express = require('../')
+, request = require('./support/http');
+
+describe('req', function(){
+ describe('.buildAbsoluteUri', function(){
+ it('should return the entire uri string', function(){
+ var app = express();
+
+ app.use(function(req,res){
+ res.end(req.buildAbsoluteUri("express.com"));
+ })
+
+ request(app)
+ .get('/absuri')
+ .end(function(res){
+ res.body.should.equal('http://express.com/absuri');
+
+ })
+
+ app.use(function(req,res){
+ res.end(req.buildAbsoluteUri());
+ })
+
+ request(app)
+ .get('/absuri')
+ .end(function(res){
+ res.body.should.match(/(|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/)
+ })
+
+ })
+ })
+
+})
Something went wrong with that request. Please try again.