A platform which allow us to run Javascript on the computer/server.
Javascript |
---|
C++ Node.JS V8 |
Machine Code |
function greet() {
console.log("Hello")
}
var goodBye = function() {
console.log("Bye")
}
var callFunction = (func) {
func()
}
callFunction(goodBye)
// File 1
function countElements(arr){
console.log('Array size is: ' + arr.lenght)
}
var myArray = ['foo', 'bar']
console.log(countElements(myArray))
// File 2
var myArray2 = ['foo', 'bar', 'foobar']
console.log(countElements(myArray2))
// File 1
module.exports = function(arr){
console.log('Array size is: ' + arr.lenght)
}
// File 2
var count = require('./File1')
var myArray2 = ['foo', 'bar', 'foobar']
count(myArray2)
// File 1
module.exports = function(arr){
console.log('Array size is: ' + arr.lenght)
}
// File 2
var count = require('./File1')
var myArray2 = ['foo', 'bar', 'foobar']
console.log(count(myArray2))
module.exports.sumar = sumar;
module.exports.restar = restar;
module.exports.multiplicar = multiplicar;
module.exports.dividir = dividir;
function sumar(a, b){
return a+b;
}
function restar(a, b){
return a-b;
}
function multiplicar(a, b){
return a*b;
}
function dividir(a, b){
return a/b;
}
var events = require('events');
var eventEmitter = new events.EventEmitter();
var greet = function (){
console.log('Hi you!');
}
eventEmitter.on('doorOpen', greet);
eventEmitter.emit('doorOpen');
var fs = require('fs');
var fs = require('fs')
var file = fs.readFileSync('fileName', 'utf-8');
console.log(file)
var fs = require('fs')
fs.writeFileSync('newFileName', 'new file');
var fs = require('fs')
fs.readFile('fileName', 'utf-8', function(err, data) {
console.log(data)
});
Request
Response
var http = require('http');
var server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type' : 'text/plain'})
res.end('Hi Nodeschool')
})
server.listen(3000, '127.0.0.1');
console.log('Listening 127.0.0.1:3000')
Temporary storage spot for a chunk of data that is being transferred from one place to another
Buffer is filled with data, then passed along
Transfer small chunks of data at a time
Flows of data you can either read from, or write to.
var fs = require('fs');
var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
myReadStream.on('data', function(chunk){
console.log('new chunk recieved')
})
var fs = require('fs');
var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
var myWriteStream = fs.createWriteStream('./write.txt');
myReadStream.on('data', function(chunk){
console.log('new chunk recieved')
myWriteStream.write(chunk)
})
var fs = require('fs');
var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
var myWriteStream = fs.createWriteStream('./write.txt');
myReadStream.pipe(myWriteStream)
var fs = require('fs');
var http = require('http');
var server = http.createServer(function(req, res){
console.log('request was made by ' + req.url)
res.writeHead(200, {'Content-Type' : 'text/plain'})
var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
myReadStream.pipe(res)
})
server.listen(3000, '127.0.0.1');
console.log('Listening 127.0.0.1:3000')