Click here to Skip to main content
15,886,258 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 357.4K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
 * MIT Licensed
 */

var vm = require('vm')
  , should = require('should');

/**
 * Generates evn variables for the vm so we can `emulate` a browser.
 * @returns {Object} evn variables
 */

exports.env = function env () {
  var details = {
      location: {
          port: 8080
        , host: 'www.example.org'
        , hostname: 'www.example.org'
        , href: 'http://www.example.org/example/'
        , pathname: '/example/'
        , protocol: 'http:'
        , search: ''
        , hash: ''
      }
    , console: {
        log:   function(){},
        info:  function(){},
        warn:  function(){},
        error: function(){}
      }
    , navigator: {
          userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit'
           + '/534.27 (KHTML, like Gecko) Chrome/12.0.716.0 Safari/534.27'
        , appName: 'socket.io'
        , platform: process.platform
        , appVersion: process.version
    , }
    , name: 'socket.io'
    , innerWidth: 1024
    , innerHeight: 768
    , length: 1
    , outerWidth: 1024
    , outerHeight: 768
    , pageXOffset: 0
    , pageYOffset: 0
    , screenX: 0
    , screenY: 0
    , screenLeft: 0
    , screenTop: 0
    , scrollX: 0
    , scrollY: 0
    , scrollTop: 0
    , scrollLeft: 0
    , screen: {
          width: 0
        , height: 0
      }
  };

  // circular references
  details.window = details.self = details.contentWindow = details;

  // callable methods
  details.Image = details.scrollTo = details.scrollBy = details.scroll = 
  details.resizeTo = details.resizeBy = details.prompt = details.print = 
  details.open = details.moveTo = details.moveBy = details.focus = 
  details.createPopup = details.confirm = details.close = details.blur = 
  details.alert = details.clearTimeout = details.clearInterval = 
  details.setInterval = details.setTimeout = details.XMLHttpRequest = 
  details.getComputedStyle = details.trigger = details.dispatchEvent = 
  details.removeEventListener = details.addEventListener = function(){};

  // frames
  details.frames = [details];

  // document
  details.document = details;
  details.document.domain = details.location.href;

  return details;
};

/**
 * Executes a script in a browser like env and returns
 * the result
 *
 * @param {String} contents The script content
 * @returns {Object} The evaluated script.
 */

exports.execute = function execute (contents) {
  var env = exports.env() 
    , script = vm.createScript(contents);

  // run the script with `browser like` globals
  script.runInNewContext(env);

  return env;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions