background preloader

A Case-by-Case Introduction to Node.js

A Case-by-Case Introduction to Node.js
Introduction JavaScript’s rising popularity has brought with it a lot of changes, and the face of web development today is dramatically different. The things that we can do on the web nowadays with JavaScript running on the server, as well as in the browser, were hard to imagine just several years ago, or were encapsulated within sandboxed environments like Flash or Java Applets. Before digging into Node.js, you might want to read up on the benefits of using JavaScript across the stack which unifies the language and data format (JSON), allowing you to optimally reuse developer resources. As Wikipedia states: “Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” After over 20 years of stateless-web based on the stateless request-response paradigm, we finally have web applications with real-time, two-way connections. How Does It Work? That’s a mouthful. The list goes on.

An Absolute Beginner's Guide to Node.js There's no shortage of Node.js tutorials out there, but most of them cover specific use cases or topics that only apply when you've already got Node up and running. I see comments every once and awhile that sound something like, "I've downloaded Node, now what?" This tutorial answers that question and explains how to get started from the very beginning. What is Node.js? A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. An important thing to realize is that Node is not a webserver. Installing Node Node.js is very easy to install. I've Installed Node, now what? Once installed you'll have access to a new command called "node". $ node > console.log('Hello World'); Hello World undefined In the above example I typed "console.log('Hello World')" into the shell and hit enter. The other way to run Node is by providing it a JavaScript file to execute. hello.js console.log('Hello World'); $ node hello.js Hello World Doing Something Useful - File I/O example_log.txt

Understanding node.js Node.js has generally caused two reactions in people I've introduced it to. Basically people either "got it" right away, or they ended up being very confused. If you have been in the second group so far, here is my attempt to explain node: It is a command line tool. You download a tarball, compile and install the source.It let's you run JavaScript programs by typing 'node my_app.js' in your terminal.The JS is executed by the V8 javascript engine (the thing that makes Google Chrome so fast).Node provides a JavaScript API to access the network and file system "But I can do everything I need in: ruby, python, php, java, ... !". I hear you. "Get to the point!" Alright, I will. "Huh?" That's right, everything runs in parallel, except your code. The day starts by one servant waking up the king and asking him if he needs anything. Once a servant finishes a task, he lines up outside the kings quarter to report. "That's fantastic, but could you quit the silly metaphor and speak geek to me?" Sure. No.

Debugger Node.js v0.10.17 Manual Stability: 3 - Stable V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear: % node debug myscript.js < debugger listening on port 5858 connecting... ok break in /home/indutny/Code/git/indutny/myscript.js:1 1 x = 5; 2 setTimeout(function () { 3 debugger; debug> Node's debugger client doesn't support the full range of commands, but simple step and inspection is possible. For example, suppose myscript.js looked like this: x = 5;setTimeout(function () { debugger; console.log("world");}, 1000); console.log("hello"); Then once the debugger is run, it will break on line 4. The repl command allows you to evaluate code remotely. Watchers# You can watch expression and variable values while debugging your code. To start watching an expression, type watch("my_expression"). watchers prints the active watchers. Commands reference# Stepping# % . Info#

Using Express.js for APIs | StrongLoop A few tips and libraries for creating and documenting RESTful APIs with Express.js. See a simple example app that incorporates some of these ideas. Two things really got me sucked into Express when I first started using it: Jade and Stylus. I can barely tolerate writing HTML and CSS any other way. These days I spend more time using Express for APIs that send nothing but JSON. In this post I’ll cover a few libraries and reminders to help keep your code organized and your API friendly for developers. Mongoose I’m assuming an API backed by MongoDB, and that we’re using Mongoose. express-mongoose I’m generally not a flow control library guy. The fact that it supports Promises is important to keep in mind, because it means you can create some fairly complex async stuff in your Mongoose models, and as long as it returns a Promise you keep the top layer routing/sending code really tidy. Mongoose Transforms I often find myself wanting to hide certain properties before they get sent across the wire.

nodeapps/http-server How To Node - NodeJS Node Tuts - Episode VIII - Handling Errors Using Domains You can navigate the video and the script by using the ← and → cursor keys. In this episode we are going to cover the usage of domains and how that can help to make your Node.js application server resilient and manageable on the event of errors. Life Before Domains Before domains were introduced in Node v0.8, the life of a Node.js server was harder. Whenever an error was thrown, the Node process would log the error and exit. 01_server_throws.js: var server = require('http').createServer(); server.on('request', function(req, res) { a.abc(); res.end('Thank you for using our service!') In this example we are forcing an error to be thrown when processing any request, but this type of error could happen occasionally, bringin your entire server down, and with it all the clients that were connected. To prevent Node.js from exiting, we could listen for the uncaughtException event on the global process object: 02_uncaught_exception_handler.js: Event Emitters and Error Handling Enter domains 05_domains.js:

Related: