Created
          April 28, 2011 20:10 
        
      - 
      
- 
        Save avalanche123/947206 to your computer and use it in GitHub Desktop. 
    module for "longening" short urls
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var resolve = require('./url_resolver'); | |
| resolve('http://bit.ly/foo', function(e, url) { | |
| if(e) { | |
| console.log(e); | |
| } | |
| console.log(url); | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var http = require('http'), | |
| urlParser = require('url'); | |
| var resolve = function(url, fn) { | |
| var parts = urlParser.parse(url), | |
| result, | |
| req = http.request({ | |
| host: parts.hostname, | |
| port: parts.port || 80, | |
| path: parts.pathname, | |
| query: parts.query || null, | |
| method: 'HEAD', | |
| headers: { | |
| 'content-length': 0 | |
| } | |
| }, | |
| function(res) { | |
| if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location) { | |
| return resolve(res.headers.location, fn); | |
| } | |
| fn(null, url); | |
| }); | |
| req.end(); | |
| req.on('error', function(e) { | |
| fn(e); | |
| }); | |
| } | |
| module.exports = resolve; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment