SnapSearch-Client-Node
Snapsearch Client Node is Node.js based framework agnostic HTTP client library for SnapSearch (https://snapsearch.io/).
Snapsearch is a search engine optimisation (SEO) and robot proxy for complex front-end javascript & AJAX enabled (potentially realtime) HTML5 web applications.
Search engines like Google's crawler and dumb HTTP clients such as Facebook's image extraction robot cannot execute complex javascript applications. Complex javascript applications include websites that utilise AngularJS, EmberJS, KnockoutJS, Dojo, Backbone.js, Ext.js, jQuery, JavascriptMVC, Meteor, SailsJS, Derby, RequireJS and much more. Basically any website that utilises javascript in order to bring in content and resources asynchronously after the page has been loaded, or utilises javascript to manipulate the page's content while the user is viewing them such as animation.
Snapsearch intercepts any requests made by search engines or robots and sends its own javascript enabled robot to extract your page's content and creates a cached snapshot. This snapshot is then passed through your own web application back to the search engine, robot or browser.
Snapsearch's robot is an automated load balanced Firefox browser. This Firefox browser is kept up to date with the nightly versions, so we'll always be able to serve the latest in HTML5 technology. Our load balancer ensures your requests won't be hampered by other user's requests.
For more details on how this works and the benefits of usage see https://snapsearch.io/
SnapSearch provides similar libraries in other languages: https://github.com/SnapSearch/Snapsearch-Clients
Installation
Install it from NPM.
npm install snapsearch-client-nodejs --save
Usage
SnapSearch's NodeJS client is broken up into 3 basic classes, Client, Detector and Interceptor. All it needs is a request object extended from NodeJS's http.IncomingMessage
prototype. This allows you to use it any NodeJS framework. However we have bundled a connect compatible middleware for ease of use. This middleware should be used at the entry/bootstrap point of your NodeJS application. There are more examples are in the examples folder.
The order of middleware setup in Express is important. For most cases, you should place the SnapSearch middleware as closest to the kernel of your application as possible. This means placing the SnapSearch middleware setup lower in the sequence of app.use
expressions. Any middleware augmenting the request and response that should be running when a normal user is accessing the site, should probably also run when SnapSearch is intercepting. Any middleware placed after SnapSearch will not run during an interception.
For full documentation on the API and API request parameters see: https://snapsearch.io/documentation
By the way, you need to blacklist non-html resources such as sitemap.xml
. This is explained in https://snapsearch.io/documentation#notes
Basic Connect/Express Usage
var express = ;var snapsearch = ; var app = ; //by default the it will only intercept and return a response with only status, header location, and html bodyapp
Here's an example response coming back from SnapSearch's API (not all variables are available, you need to check your request parameters):
"code": "success" "content": "cache" true/false "callbackResult" "" "date" 1390382314 "headers" "name" "Content-Type" "value" "text/html" "html" "<html></html>" "message" "Success/Failed/Validation Errors" "pageErrors" "error" "Error: document.querySelector(...) is null" "trace" "file" "filename" "function" "anonymous" "line" "41" "sourceURL" "urltofile" "screenshot" "BASE64 ENCODED IMAGE CONTENT" "status" 200
Advanced Connect/Express Usage
var express = ;var snapsearch = ; var app = ; app
Advanced Usage
The below shows how you can manipulate the properties and parameters of the client, detector and interceptor. The example shows them being applied on a simple HTTP server, however the principle is the same with Express/Connect integrations.
var http = ;var snapsearch = ; var apiRequestParameters = //add your API request parameters if you have any...; var blackListedRoutes = //add your black listed routes in regex if you have any... //for example: '^/sitemap.xml'; var whiteListedRoutes = //add your white listed routes in regex if you have any...; var checkFileExtensions = false; //if you wish for SnapSearch Client to check if the URL leads to a static file, switch this on to a boolean true, however this is expensive and time consuming, so it's better to use black listed or white listed routes var trustedProxy = false; //if you are behind a reverse proxy, switch this to true so we can acquire the real protocol from X-Forwarded-Proto header var pathToCustomRobotsJson = ''; //custom robots json path var pathToCustomExtensionsJson = ''; //custom extensions json path var client = 'ENTER YOUR EMAIL' 'ENTER YOUR KEY' apiRequestParameters { // mandatory console; console; }; var detector = blackListedRoutes whiteListedRoutes checkFileExtensions trustedProxy pathToCustomRobotsJson pathToCustomExtensionsJson; var interceptor = client detector; //robots can be direct accessed and manipulateddetectorrobotsignore;detectorrobotsmatch; //extensions can as well, add to 'generic' or 'js'detectorextensionsgeneric;detectorextensionsjs = 'valid js extension'; //there is currently no js extensions //the beforeIntercept callback is called after the Detector has detected a search engine robot//if this callback returns an object, the object will be used as the response to interceptor.intercept//use it for client side caching in order to have millisecond responses to search engines//the afterIntercept callback can be used to store the snapshot from SnapSearch as a client side cached resource//this is of course optional as SnapSearch caches your snapshot as well!clientCache = ;interceptor; http;
That's pretty much it. Check the source code for more, it's tiny and well documented.
Reverse Proxies
If you are behind a reverse proxy such as NGINX, Apache or certain PAAS providers (Heroku), certain HTTP information between the client and the proxy needs to be explicitly passed by the proxy to the backend, this includes the HTTP protocol and hostname. This information is usually passed in X-Forwarded-Proto
& X-Forwarded-Host
headers. Because these headers can be forged, by default we do not automatically trust these headers. But if you control the proxy, and you trust these headers, then you need to switch true the trustedProxy
boolean flag in the Detector
function constructor. You may call it like:
var trustedProxy = true; var detector = false trustedProxy;
If you use Express, you may also need to enable app.enable('trust proxy');
. See http://expressjs.com/guide/behind-proxies.html
We have a demo of a working SnapSearch enabled express application running on Heroku.
Development
Install/update all dependencies:
npm install
Make your changes. Then use this to create a version in the package.json, ti also creates a new git tag.
npm version [<newversion> | major | minor | patch] -m "New release"
Synchronise and push the tag with:
git push
git push --tags
Push the version to NPM:
npm publish
Tests
Unit tests are written using Mocha and Chai. To run tests use npm test
.
To run tests in Windows use ./node_modules/.bin/mocha --reporter spec
.