On Github AntonNarkevich / nodeIntroDoc
Do you hate it?
V8 is Google's open source JavaScript engine.
V8 is written in C++ and is used in Google Chrome.
V8 implements ECMAScript, 5th edition...
It's written in C++ and totally open source.
Node.js core consists of 30 .cpp files.
Some default modules are written in JavaScript.
var http = require('http'); http.createServer(function (req, res) { res.end('Hello World\n'); }).listen(987); console.log('Server running at localhost:987');
var fs = require("fs"); fs.readFile("foo.txt", "utf8", function(error, data) { console.log(data); });
var connection = require('mysql').createConnection({ host: 'localhost', user: 'userName', password: 'secret', database: 'databaseName' }).connect(); connection.queryRow('SELECT * FROM _Language where LanguageId=3', function(err, row) { console.log(data); } );
async.series([ function(callback){ // do some stuff ... callback(null, 'one'); }, function(callback){ // do some more stuff ... callback(null, 'two'); } ], // optional callback function(err, results){ // results is now equal to ['one', 'two'] });
async.parallel([ function(callback){ setTimeout(function(){ callback(null, 'one'); }, 200); }, function(callback){ setTimeout(function(){ callback(null, 'two'); }, 100); }], function(err, results){ // After all. The results array will equal ['one','two'] });
Total Packages: 69 629
jshint: { // define the files to lint files: ['gruntfile.js', 'src/**/*.js', 'test/**/*.js'], // configure JSHint (documented at http://www.jshint.com/docs/) options: { // more options here if you want to override JSHint defaults globals: { jQuery: true, console: true, module: true } } }
qunit: { files: ['test/**/*.html'] },
var cp = require('child_process'); var n = cp.fork(__dirname + '/sub.js'); n.on('message', function(m) { console.log('PARENT got message:', m); }); n.send({ hello: 'world' });
process.on('message', function(m) { console.log('CHILD got message:', m); }); process.send({ foo: 'bar' });
var cluster = require('cluster'); var numCPUs = require('os').cpus().length; var http = requier('http'); if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } else { http.createServer(function(req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000); }
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
app.get('/hello.txt', function(req, res){ res.send('Hello World'); });
<ul> <% for(var i=0; i<supplies.length; i++) {%> <li><%= supplies[i] %></li> <% } %> </ul>