Entries in Node.js (1)

Monday
Dec162013

Resource for SharePoint farm upgrade with server name change

This was a question I had someone ask last week regarding how you might handle the situation where you want to change the host name of your SharePoint farm in an upgrade (or just the host name for any web app really) knowing that all embedded links will be broken.

 

The low hanging fruit is to set a DNS record so that the old and new point to your new server address, but if you want to alter some site names and/or alert users that they have an old, deprecated url instead of just forwarding them on, the solution below might work for you.

 

The following script is for the node.js platform and, if run on a server with your old host name's resolution, will intercept incoming http requests, map it to your new server, allowing for site renaming and provide the user with a message as well as the new 'best guess' url to what they were trying to reach.  It is pretty straightforward and should be relatively easy to modify as needed.

 

HTTPS (443): This is geared toward standard HTTP traffic, if you need a version to work with HTTPS on port 443, you can find some subtle changes that will be needed in the node.js information here http://nodejs.org/api/https.html.

 

// Kevin Guyer - 2013
// Node.js script to handle server (SharePoint) name changes
//  This is designed to catch the use of old urls and present an infomational page with the new url to the resource
//  Test with this to see URL altering:  http://localhost:8080/hr/foosite/barsite/somelibrary/somedoc.pdf

// Intercept incoming port 80 or 443 (needs to operate on both) - set the port below for flavor you are running
var http = require('http');
var url = require("url");
var server = http.createServer();

server.on('request', function(request, response) {
	var hostname = request.headers.host;
    var newHostname = 'https://yournewfarmpath';  // no trailing slash, set to your new farm/server name and path
    var pathname = url.parse(request.url).pathname;
    var newPathname = pathname;
    var query = url.parse(request.url).query;  // optional, not used yet
	
    // Run match logic on array elements to determine what needs to be displayed
    var aResPath = pathname.split('/');
    
    // Modify path elements as needed with element matching:
    var wasAltered = false;
	if(aResPath.length >=1){
		try{
			if(aResPath[1].toLowerCase() == 'hr'){ aResPath[1] = 'HumanResources'; wasAltered = true; }
			if(aResPath[3].toLowerCase() == 'barsite'){ aResPath[3] = 'NewBarSite'; wasAltered = true; }
			// add others as needed, sites on root starting at index [1]...
		} 
		catch(err) {
			// handle as desired...
		}
		
		if(wasAltered == true){
			console.log("Detected swap string in path, altering output.");
			newPathname = '';
			for(i = 1; i < aResPath.length; i++){
				newPathname = newPathname + '/' + aResPath[i];
			}
		}
	}
    // Build response message
    var payload = '<h2>Oops.</h2><p>The url you attempted to reach (<em>' + hostname +  pathname + '</em>) is no longer valid.</p>';
    // Optional: Build and supply a best bet url    
    payload += '<p>Our best guess for the new url for this resource is <a href="'+ newHostname + newPathname+'">'+ newHostname + newPathname+'</a>.</p>';
    payload += '<p>Please update your favorite or link source to reflect this new address.</p>';

    // Optional step to log the old url hit via a web API (REST) - todo...

    // Build and deliver response    
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(payload);
    response.end();
});

var port = 8080;  // change this to 80 or 443 as needed
server.listen(port);
server.once('listening', function() {
    console.log('Redirect server is now listening on port %d', port);
});