background preloader

NodeJs

Facebook Twitter

Scalable Node.js application architecture. Comparing Express, Restify, hapi and LoopBack for building RESTful APIs. If you are writing a Node.js application, chances are you going to have some kind of API end points to be consumed by your front end or expose data for others to take in.

Comparing Express, Restify, hapi and LoopBack for building RESTful APIs

This is where RESTful APIs come in. And now you have a dilemma – what tools to use and what approach to take? So many choices… Thanks to the incredibly active Node.js community, the amount of results for a rest search on NPM is pretty overwhelming. Everyone has their own implementation and approach, but few seem to agree on a common way to go about implementing RESTful APIs in Node.js. RESTful APIs with Express The most common approach is to just roll your own end points with Express. Example Here’s what a typical end point might look like in Express using the latest 4.x Router feature: Pros Little learning curve, Express is nearly a standard for Node.js web applicationFully customizable Cons Express is a great starting point, but eventually you will feel the pain of “roll your own” approach. RESTful APIs with Restify LoopBack.

10 Habits of a Happy Node Hacker (2016) At the tail end of 2015, JavaScript developers have a glut of tools at our disposal.

10 Habits of a Happy Node Hacker (2016)

The last time we looked into this, the modern JS landscape was just emerging. Today, it's easy to get lost in our huge ecosystem, so successful teams follow guidelines to make the most of their time and keep their projects healthy. Here are ten habits for happy Node.js hackers as we enter 2016. They're specifically for app developers, rather than module authors, since those groups have different goals and constraints: 1. Npm's init command will scaffold out a valid package.json for your project, inferring common properties from the working directory. $ mkdir my-awesome-app $ cd my-awesome-app $ npm init --yes I'm lazy, so I run it with the --yes flag and then open package.json to make changes. 2. By default, npm doesn't save installed dependencies to package.json (and you should always track your dependencies!). One solution is installing packages like this: $ npm install foobar --save --save-exact.

11. File system - Mixu's Node book. This chapter covers the file system module.

11. File system - Mixu's Node book

The file system functions consist of file I/O and directory I/O functions. All of the file system functions offer both synchronous (blocking) and asynchronous (non-blocking) versions. The difference between these two is that the synchronous functions (which have “Sync” in their name) return the value directly and prevent Node from executing any code while the I/O operation is being performed: var fs = require('fs');var data = fs.readFileSync('. /index.html', 'utf8');// wait for the result, then use it console.log(data); Asynchronous functions return the value as a parameter to a callback given to them: var fs = require('fs'); fs.readFile('. The table below lists all the asynchronous functions in the FS API.

You should use the asynchronous version in most cases, but in rare cases (e.g. reading configuration files when starting a server) the synchronous version is more appropriate. Scaling Node.js Applications. Scaling Node.js applications can be a challenge.

Scaling Node.js Applications

JavaScript’s single threaded nature prevents Node from taking advantage of modern multi-core machines. For example, the following code implements a bare bones HTTP server, which listens on the port number passed in from the command line. This code will execute in a single thread, whether it’s run on a single core machine or a 1,000 core machine. Taking Advantage of Multiple Cores With a little work, the previous code can be modified to utilize all of the available cores on a machine. Scaling Across Machines Nodejitsu has developed node-http-proxy, an open source proxy server for Node applications. Npm install http-proxy The actual reverse proxy server is shown below. Of course, this example only uses one machine. Scaling Using nginx Using a Node reverse proxy is nice because it keeps your entire software stack in the same technology. As I mentioned, for the purposes of this article, we are only interested in a few pieces of the file. What Makes Node.js Faster Than Java? Every few weeks someone posts a Java vs Node benchmark, like PayPal’s or Joey Whelan’s.

What Makes Node.js Faster Than Java?

As one of maintainers of Node core and contributors to many npm modules, StrongLoop is happy to see Node winning lately. Understanding the node.js event loop. So the largest waste with current programming technologies comes from waiting for I/O to complete.

Understanding the node.js event loop

There are several ways in which one can deal with the performance impact (from Sam Rushing): The second basis thesis is that thread-per-connection is memory-expensive: [e.g. that graph everyone showns about Apache sucking up memory compared to Nginx] Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). You can see how that overhead eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients.