background preloader

Javascript

Facebook Twitter

How to Unit Test Private Functions in JavaScript — Philip Walton. Update: A lot of readers have commented or tweeted that I shouldn’t be unit testing private functions in the first place. Admittedly, this article is a little light on the why and mainly focuses on the how. JavaScript’s closures provide an excellent way to make variables and functions private, keeping them out of the global scope. This is particularly important in the browser because all scripts share the same scope, and it’s quite easy to inadvertently pick a variable or function name used by another library. The problem, however, is that when functions are hidden inside a closure, it’s very difficult to test them. Here’s an example: The immediately invoked function expression returns an object that exposes the bar function globally, but the foo function remains inaccessible to any code not appearing inside the immediately invoked function.

In other words, given the above code, writing a unit test for bar is easy, but writing a unit test for foo is impossible. My Solution A Word of Caution. Roy Jin. Snap.svg Tutorial. The Node Beginner Book » A comprehensive Node.js tutorial. Simple REST Service with Node.js and Express - QAT Global. Introduction This is a simple tutorial showing how to create a REST service with Node.js using Express.

This first tutorial will assume you have already installed Node.js, but it will walk you through installing Express and setting up a project. We will also show how to setup a simple REST service and how you can test it. In follow-up tutorials, we will show how to add a web page interface for interacting with the service, add authentication to the service and how to integrate with MongoDB for persistence. If you are unfamiliar with Node.js, please visit their official website. The code for this tutorial is available here on github. Setup The Environment If you don't already have express installed, you can install it with the following command: npm install express -g You will want to create a new directory for your project, so create your project directory and then from within that directory, run the following command to create your new project shell. express npm install -d node app.js app. app.

6 things you should know about Node.js. JavaScript is eating the world, with new tools and enhancements arriving at a breakneck pace. With Node.js, an open source runtime system invented in 2009 by Ryan Dahl, that reach has extended to the server side. Node.js has become wildly popular, with coders everywhere using it to create APIs and build a new matrix of interoperability across the Internet. Joyent has been the chief sponsor of Node.js from the beginning. In this week's New Tech Forum, Ben Wen, vice president of product marketing at Joyent, outlines six things you should know about the phenomenon shaking up backend development. -- Paul Venezia Node.js is a runtime system for creating (mostly) server-side applications. It's best known as a popular means for JavaScript coders to build real-time Web APIs. But Node.js is not a JavaScript framework; indeed, several authors have written excellent frameworks specifically for Node.js, including Express.js, Restify.js, and Hapi.js. 1. 2. 3. 4. 5. 6.

Node in context. Authentication with AngularJS and a Node.js REST api | Kevin Delemme - Software Engineer. Geek. JavaScript Addict. Few months ago, I got intrigued by AngularJS and I decided to start learning it by building some really simple applications. BlogJS is one of them. Blogjs is a simple blog written with AngularJS, Node.js and MongoDB. You can found a working demo of the front end here and the administration part is accessible here. The username is demo and the password is demo.

You can check the code on github. Purpose The goal of this project is to learn how to build an authentication and authorization mecanism with AngularJS and a RESTful api running on a Node.js server. When a user sends his credentials, the Node.js server checks that they are correct and answers with a unique token built with the user informations. Features Create articlesEdit articlesDelete articlesPublish articlesUnpublish articlesDisplay articles per dateDisplay articles per tagAuthentication & Authorization Stack AngularJSNode.js with express.js, express-jwt and mongooseMongoDB Authentication with AngularJS.

Build a RESTful API Using Node and Express 4. Express Router and Routes We will use an instance of the Express Router to handle all of our routes. Here is an overview of the routes we will require, what they will do, and the HTTP Verb used to access it. This will cover the basic routes needed for an API. This also keeps to a good format where we have kept the actions we need to execute (GET, POST, PUT, and DELETE) as HTTP verbs. Route Middleware We’ve already defined our first route and seen it in action. Let’s say that we wanted something to happen every time a request was sent to our API. All we needed to do to declare that middleware was to use router.use(function()). We are sending back information as JSON data.

We will also add next() to indicate to our application that it should continue to the other routes. Middleware Uses Using middleware like this can be very powerful. Testing Our Middleware With middleware, we can do awesome things to requests coming into our API. Creating the Basic Routes Creating a Bear POST /api/bears. Learning JavaScript Design Patterns. Design patterns are reusable solutions to commonly occurring problems in software design. They are both exciting and a fascinating topic to explore in any programming language. One reason for this is that they help us build upon the combined experience of many developers that came before us and ensure we structure our code in an optimized way, meeting the needs of problems we're attempting to solve. Design patterns also provide us a common vocabulary to describe solutions. This can be significantly simpler than describing syntax and semantics when we're attempting to convey a way of structuring a solution in code form to others.

In this book we will explore applying both classical and modern design patterns to the JavaScript programming language. Target Audience This book is targeted at professional developers wishing to improve their knowledge of design patterns and how they can be applied to the JavaScript programming language. Acknowledgments Credits Reading We already use patterns everyday. Javascript. Optical Character Recognition in Javascript. Ocrad.js is a pure-javascript version of the Ocrad project, automatically converted using Emscripten. It is a simple OCR (Optical Character Recognition) program that can convert scanned images of text back into text. Clocking in at about a megabyte of Javascript with no hefty training data dependencies (looking at you, Tesseract), it's on the lighter end of the spectrum.

This was made by antimatter15 (please follow me on Twitter or G+) Below is a simple demo, which should hopefully demonstrate the capabilities but will more likely show the substantial limitations of the library. Hit the buttons on the left to reset the canvas or to randomly put some text in a random font. You can also try to draw something with your cursor. The Ocrad.js API is really simple. This file exposes a single global function, OCRAD which takes an image as an argument and returns the recognized text as a string. var string = OCRAD(image); alert(string); What about GOCR.js? Browsing to a Simple Web Server on a Mac from Sublime Text. Lately one of the computers I have been using is a Mac Air with the Haswell chip. The combination of the Mac Air’s sleek and lightweight design along with the super long life and power of the Haswell chip is just fantastic! I’m using both OSX and Windows (in Parallels) and I spend most of my time on the Air in Xcode, Sublime Text, and Visual Studio (also tinkering with AppCode from Jet Brains … but more on that later).

When I want to write and test some quick HTML/JavaScript/CSS that requires a web server on the Mac Air I use Sublime Text. Sublime is pretty cool as an editor and the plug-in ecosystem is reach. But many of the plug-ins lack polish, some don’t seem to work well, and it can be daunting to know which one to choose at times. Overall I love the plug in model, but its very similar to Visual Studio’s in that you need to try them out to know which you like. So why did I go down this road? I found a quick and easy way to solve this with some built in OS features on the Mac. Why? Http-server. Http-server is a simple, zero-configuration command-line http server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development, and learning.

Installation via npm. If you don't have npm yet: curl https: Once you have npm: npm install http-server -g This will install http-server globally so that it may be run from the command line. Usage: http-server [path] [options] [path] defaults to . mkdir myapp cd myapp/ jitsu install http-server If you do not have jitsu installed you can install it via npm install jitsu -g Usage Starting http-server locally node bin/http-server Now you can visit to view your server Deploy http-server to nodejitsu jitsu deploy You will now be prompted for a subdomain to deploy your application on Available Options: -p Port to listen for connections on (defaults to 8080) -a Address to bind to (defaults to '0.0.0.0') -d Show directory listings (defaults to 'True') JavaScript Garden. Function Declarations and Expressions Functions in JavaScript are first class objects.

That means they can be passed around like any other value. One common use of this feature is to pass an anonymous function as a callback to another, possibly an asynchronous function. The function Declaration function foo() {} The above function gets hoisted before the execution of the program starts; thus, it is available everywhere in the scope it was defined, even if called before the actual definition in the source. foo(); // Works because foo was created before this code runsfunction foo() {} The function Expression var foo = function() {}; This example assigns the unnamed and anonymous function to the variable foo. foo; // 'undefined'foo(); // this raises a TypeErrorvar foo = function() {}; Due to the fact that var is a declaration that hoists the variable name foo before the actual execution of the code starts, foo is already declared when the script gets executed.

Named Function Expression How this Works. Map. Edit fiddle - JSFiddle. JavaScript Objects. Pen. Draggabilly - back handle. Implementing Native Drag and Drop. Drag and Drop is one of those interactions that can really help to make an interface simple to use. There are plenty of JavaScript libraries that can be used to create drag and drop interfaces but what many people don’t know is that all of the major browsers actually have native support for drag and drop. In this blog post you are going to learn how to make use of the native Drag and Drop API in order to create your own Drag and Drop interfaces. Making Elements Draggable To get us started we are first going to take a look at how to make HTML elements draggable. This is done using the draggable attribute.

Setting the value of the draggable attribute to true informs the browser that this element can be dragged. <div draggable="true">Draggable Div</div> Note: Some elements such as <a> and <img> are draggable by default in many browsers. Listening for Drag Events There are a number of events that are fired during a drag interaction. The DataTransfer Object Drag and Drop with Page Elements.