Node.js
  • Introduction
  • Sandbox
Powered by GitBook
On this page
  • What is Node.js?
  • Node Package Manager - NPM
  • Installing Node.js
  • First server
  • Creating a complete server on the Edison
  • Cylon.js

Sandbox

PreviousIntroduction

Last updated 7 years ago

What is Node.js?

JavaScript on the server. Google's V8 engine. Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications. Node.js is not a JavaScript framework, but its applications are written in JavaScript and can be run within the Node.js runtime on a wide variety of platforms, including FreeBSD, IBM AIX, IBM i, IBM System z, Linux, Microsoft Windows, NonStop and OS X. Node.js is used by GoDaddy, Groupon, IBM, LinkedIn, Microsoft, Netflix, PayPal, Walmart, and Yahoo!. Node.js is typically used where light-weight, real-time response is needed, like communication apps and Web-based gaming. It is used to build large, scalable network applications. Runs with a single thread in a process, no blocking I/O, asynchronous model.

Capabilities

Node is a platform for writing JavaScript applications outside web browsers. This is not the JavaScript we are familiar with in web browsers!. There is no DOM built into Node, nor any other browser capability. Beyond its native ability to execute JavaScript, the bundled modules provide the following capabilities: Command-line tools (in shell script style) Interactive-TTY style of program (REPL or Read-Eval-Print Loop) Excellent process control functions to oversee child processes File system access *Built-in rudimentary unit testing support through assertions

Node Package Manager - NPM

To start watching NPM in action, let's first create a package.json file. This file serves as a local package manifest, contains JSON (an object serialization format that is a subset of JavaScript), is placed at the root of your application folder and is, among other things, where you can declare what NPM modules your application depends on.

Installing Node.js

First server

It's very easy to write an HTTP server in Node, but the typical web application developer doesn't need to work at that level of detail. Open a simple text editor such as nano or vim, and copy & paste the code below and save it in a server.js named file. var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at ');

To run the server, type node server, node should open in the specified port.

Creating a complete server on the Edison

Status:Creating the server, since the way is creating is a full example, I'm creating a fit example, will delay a day or two to upload every step and finished code.

The MEAN stack

Generating a Node server with Express

Is a web app framework for Node Http server Routes Middleware Create a working directory with mkdir ExpressTest and switch to it cd ExpressTest. Install express globally with npm install -g express and npm install -g express-generator. Generally, we install npm modules with npm install <name_of_the_module>, but to install packages globally use the -g flag. Generate a server using express in the current directory of ExpressTest. This will generate a full node server with routes and http methods.

Alternatives

Node-RED

Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.

Prerequisites

Node.js already installed. For Linux/OS X, if you already have Node.js installed, run: $ sudo npm install -g node-red $ node-red

Cylon.js

Cylon.js is a JavaScript framework for robotics, physical computing, and the Internet of Things. It makes it incredibly easy to command robots and devices.

All you need to get started is the cylon NPM module:

npm install cylon

https://nodejs.org/en/download/
https://nodejs.org/en/download/package-manager/
http://127.0.0.1:8124/