Node.js

Blocking vs non-blocking

A blocking operation:


console.log('Hello ' + write('Dribbit', 1000));

function write(text, delay) {
    var start = new Date().getTime();
    while (new Date().getTime() - start < delay) {
        // Waiting...
    }
    return text;
}

console.log('End log');

Install MySQL on Node.js

Add mysql dependency in the package.json file in the root of your project. Example of a package.json file:


{
  "name": "Dribbit",
  "description": "My default description",
  "private": true,
  "dependencies": {
    "mysql": "~2.0.0"
  },
  "repository": {
    "type": "git",
    "url": "git@bitbucket.org:UserName/repo.git"
  }
}

After saving package.json, run

npm install 

Create a Node.js webserver

To create a node webserver, the core http module needs to be included. To include an existing module you can use the require function.


var http = require("http");

http.createServer(function(request, response) {
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("Luke, I am your server");
    response.end();
}).listen(8888);

See the difference with a node server using express

Pages

Subscribe to RSS - Node.js