On Github bhtz / clermontjs-nodejs-slides
console.log('unleash javascript !');
var fs = require('fs'); fs.stat('/etc/passwd', function(err, stats) { if (err) return; if (stats.isFile()) { fs.readFile('/etc/passwd', function(err, data) { if (err) throw err; console.log(data); }); } });
Best package manager eveeeeer !
npm install express -gUse it :
express myProject --ejs
npm install ejsUse it :
var ejs = require('ejs');
{ "name": "microscope-kernel", "version": "0.0.1", "description": "microscopejs generators engine base librairy", "scripts": { "test": "grunt test" }, "author": "benjamin Heintz", "license": "MIT", "dependencies": { "commander": "~2.1.0", "inquirer": "~0.4.0", "ejs": "~0.8.5", "colors": "~0.6.2", "fs-extra": "~0.8.1", "lodash": "~2.4.1" }, "devDependencies": { "chai": "^1.9.1", "grunt": "^0.4.5", "grunt-contrib-clean": "^0.5.0", "grunt-mocha-test": "^0.10.2", "grunt-contrib-jshint": "~0.10.0", "sinon": "~1.10.2", "sinon-chai": "~2.5.0" } }
CommonJS
var PI = Math.PI; exports.area = function (r) { return PI * r * r; }; exports.circumference = function (r) { return 2 * PI * r; };
var circle = require('./circle.js'); var area = circle.area(4); console.log(area);
var Person = function(name, age){ this.name = name; this.age = age; }; Person.prototype.sayHello = function(){ console.log('hello ' + this.name + ', '+ this.age +' years old'); }; module.exports = Person;
var Person = require('./Person.js'); var person = new Person('bhtz', 26); person.sayHello();
http server in standard library.
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(3000); console.log('Adresse du serveur: http://localhost:3000/');
// Load the net module to create a tcp server. var net = require('net'); // Setup a tcp server var server = net.createServer(function (socket) { // Every time someone connects, tell them hello and then close the connection. socket.addListener("connect", function () { console.log("Connection from " + socket.remoteAddress); socket.end("Hello World\n"); }); }); // Fire up the server bound to port 7000 on localhost server.listen(7000, "localhost");
web application framework for node
var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); app.get('/article/show/:id', function(req, res){ db.Articles.find(req.params.id, function(article){ res.json(article); }); }); app.listen(3000);
Awesome middlewares !
doctype html html(lang="en") head title= pageTitle script(type='text/javascript'). if (foo) { bar(1 + 5) } body h1 Jade - node template engine #container.col if youAreUsingJade p You are amazing else p Get on it!
"featuring the fastest and most reliable real-time engine"
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); io.on('connection', function(socket){ console.log('a user connected'); socket.on('message', function(msg){ console.log('message: ' + msg); }); socket.on('disconnect', function(){ console.log('user disconnected'); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });
JavaScript .... seriously ?!!