SlideShare a Scribd company logo
1 of 40
Download to read offline
// lib/awesome.js
module.exports = function() {
// ... our logic here
}
// app.js
var awesome = require('./lib/awesome');
//package.json
{
"name":"project-name",
"version":"0.1.7",
"description":"...",
"dependencies":{
"other-module":"1.1.0"
}
}
//terminal
npmpublish
//terminal
npminstallproject-name
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
});
server.listen(5060, '127.0.0.1');
varfs=require('fs'),http=require('http');
varhtml=fs.readFileSync('path/to/my/file.html');
varcss=...
varserver=http.createServer(function(req,res){
varextension=...
switch(extension){
case"css":contentType="text/css";break;
case"html":contentType="text/html";break;
case"js":contentType="application/javascript";break;
case"ico":contentType="image/ico";break;
default:contentType="text/plain";
}
res.writeHead(200,{'Content-Type':contentType});
res.end(content+'n');
});
server.listen(5060,'127.0.0.1');
varhttp=require('http');
http.createServer(function(req,res){
if(req.url==='/api'){
res.writeHead(200,{'Content-Type':'application/json'});
switch(req.method){
case'GET':res.end('{"get":"OK"}');break;
case'POST':res.end('{"post":"OK"}');break;
case'PUT':res.end('{"put":"OK"}');break;
case'DELETE':res.end('{"delete":"OK"}');break;
}
}else{
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('<html><body>...n');
}
}).listen(5060,'127.0.0.1');
console.log('Serverrunningathttp://127.0.0.1:5060/');
// gulpfile.js
var gulp = require('gulp');
var concat = require('gulp-concat');
gulp.task('scripts', function() {
gulp.src('./lib/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('./dist/'))
});
// terminal
gulp scripts
//Gruntfile.js
module.exports=function(grunt){
grunt.initConfig({
concat:{
javascript:{
options:{},
src:['./lib/*.js'],
dest:'build/scripts.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default',['concat']);
}
//terminal
grunt
describe("TestingTODOMVC",function(){
before(function(){
app.todos.reset();
});
it("AddingnewTODOs",function(){
//...dosomething
expect($('#todo-listli').length).to.be.equal(2);
});
});
//server
vario=require('socket.io')(http);
io.on('connection',function(socket){
socket.on('message-type',function(msg){
socket.emit('message-received','OK');
});
});
//browser
//<scriptsrc="/socket.io/socket.io.js"></script>
varsocket=io();
socket.emit('message-type','Helloworld!');
Using Node.js for everything or what it is to write a book about it
Using Node.js for everything or what it is to write a book about it
Using Node.js for everything or what it is to write a book about it
Using Node.js for everything or what it is to write a book about it

More Related Content

Viewers also liked

Reactjs - the good, the bad and the ugly
Reactjs - the good, the bad and the uglyReactjs - the good, the bad and the ugly
Reactjs - the good, the bad and the uglyKrasimir Tsonev
 
Javascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To TailJavascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To TailCliffano Subagio
 
5 Tips for Writing Better JavaScript
5 Tips for Writing Better JavaScript5 Tips for Writing Better JavaScript
5 Tips for Writing Better JavaScriptNael El Shawwa
 
Developing large scale JavaScript applications
Developing large scale JavaScript applicationsDeveloping large scale JavaScript applications
Developing large scale JavaScript applicationsMilan Korsos
 
jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture Jiby John
 
Unidirectional data flow
Unidirectional data flowUnidirectional data flow
Unidirectional data flowDenis Gorbunov
 

Viewers also liked (7)

Reactjs - the good, the bad and the ugly
Reactjs - the good, the bad and the uglyReactjs - the good, the bad and the ugly
Reactjs - the good, the bad and the ugly
 
Javascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To TailJavascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To Tail
 
5 Tips for Writing Better JavaScript
5 Tips for Writing Better JavaScript5 Tips for Writing Better JavaScript
5 Tips for Writing Better JavaScript
 
Developing large scale JavaScript applications
Developing large scale JavaScript applicationsDeveloping large scale JavaScript applications
Developing large scale JavaScript applications
 
jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture
 
Unidirectional data flow
Unidirectional data flowUnidirectional data flow
Unidirectional data flow
 
Modern Web Applications
Modern Web ApplicationsModern Web Applications
Modern Web Applications
 

Using Node.js for everything or what it is to write a book about it