Node.js is a platform built on Chrome’s JavaScript runtime for
easily building fast, scalable network applications.Node.js uses an
event-driven, non-blocking I/O model that makes it lightweight and
efficient, perfect for data-intensive real-time applications that run
across distributed devices.
thread.
SYNCHRONOUS
$db = $this->getDatabase();
$query = 'SELECT giant_field, COUNT(field) AS counter
FROM massive_table mt
LEFT JOIN even_bigger_table ebt ON mt.field1 = ebt.field2
WHERE ebt.field3 = \'Zosia\'
ORDER BY mt.date DESC
HAVING counter > 1000';
$result = $db->query($query)->fetchAll();
WHAT IS THE SOFTWARE DOING WHILE IT QUERIES A DATABASE?
NOTHING
IT JUST WAITS FOR THE RESPONSE...
Process
request
Query
Database
Fetching
Result
Process
Response
Send
Response
WHAT SERVER'S CPU IS DOING
SERVER IS WAITING FOR THE I/O
A Read-Eval-Print-Loop (REPL) stand alone program comes with node and can be run via command line.
➜ node
> 2 + 4
6
> console.log("Hello")
"Hello"
> function sayHi(message){console.log(message)}
undefined
> sayHi("Hi There")
"Hi There"
A Node.js file is just a Javascript file. It's identical in most ways to client-side Javascript files. Example:
var testing = 12345;
function printStuff(stuff){
console.log(stuff);
}
printStuff(testing);
Execute a JS file with Node.js:
➜ ~ node app.js
// create module hello.js
var hello = function(){
console.log('hello!')
};
module.exports = hello;
// use the module in your main server file
var hello = require('./hello');
hello();
To expose your module of javascript code for other files to import/use, you need to use the 'module.exports' command:
//superhero.js
var superhero = {
saveTheDay: function (arg1, arg2, arg3){
//do something supernatural
//display that supernatural thing
}
}
module.exports = superhero;
require('./relative/path/to/module');
This module can now be used in your file:
var instanceOfSuperhero = require('./js/superhero');
instanceOfSuperhero.saveTheDay('Location', 'Time', 'Person');
Core Node Modules (Like 'http'):
require('http');
Your custom modules
require('./relative/path/to/module');
3rd party modules installed via NPM (stored in 'node_modules'' folder):
require('express');
//Import Node's http module
var http = require('http');
//create a server
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'}); //creating a response header
res.end('Hello World\n'); // creating response body.
})
.listen(3000); // open port 3000 to recieve requests and keep server listening
// Reminder for us what port we setup our server on
console.log('Server running on port 3000');
{
"name": "browserify",
"version": "10.2.4",
"description": "browser-side require() the node way",
"main": "index.js",
"repository": {
"type": "git",
"url": "http://github.com/substack/node-browserify.git"
},
"keywords": [
"browser"
],
"dependencies": {
"browser-pack": "^5.0.0",
"vm-browserify": "~0.0.1",
"xtend": "^4.0.0"
},
"author": {
"name": "James Halliday",
},
"scripts": {
"test": "tap test/*.js"
}
}
To create, use 'npm init' command:
➜ ~ npm init
Follow the prompts from there to create your file.
To download a package to your local machine:
npm install packagename
Install multiple packages:
npm install package1 package2 package3 etc
To record packages to 'package.json' file:
npm install packagename --save
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000);
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log(`Example app listening at http://${host}:${port}`);
});
Another example
Installing Express with NPM
npm install express --save
The '--save' flag will save this as a dependency in your 'package.json' file. Open it up to check it out
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.use(bodyParser.json()); // for parsing application/json
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/get_method', function (req, res) {
res.send(`Hello ${req.query.name}`);
});
var server = app.listen(3000, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log(`Example app listening at http://${host}:${port}`);
});
https://nodejs.org Node.js
http://learn.javascript.ru/screencast/nodejs Скринкаст NODE.JS
http://nodebeginner.ru/ Node.js для начинающих
http://theasder.github.io/tutorial/2014/05/16/beginners-guide-to-nodejs.html Руководство для начинающих в Node.js
http://thinking.bohdanvorona.name/to-learn-node-js/ С чего начать изучение Node.js
http://nodeguide.ru/doc/ Руководства по Node.js