This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/users/:userId', (req, res) => { | |
res.send(req.params); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/', (req, res) => { | |
res.send('Hello World!'); | |
}); | |
app.post('/', (req, res) => { | |
res.send('Got a POST request'); | |
}); | |
app.put('/user', (req, res) => { | |
res.send('Got a PUT request at /user'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send('Hello World!'); | |
}); | |
app.listen(3000, () => { | |
console.log('Example app listening on port 3000!'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// metodo async | |
function strictAddition(x, y, callback) { | |
if (typeof x !== 'number') { | |
return callback( new Error('First argument is not a number') ); | |
} | |
if (typeof y !== 'number') { | |
return callback( new Error('Second argument is not a number') ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
fs.readFile('test.txt', (err, data) => { | |
if (err) { | |
return console.error(err); | |
} | |
console.log('Async read: ' + data.toString()); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
fs.writeFile('test.txt', "Testing...", (err) => { | |
if (err) { | |
return console.log(err); | |
} | |
console.log('File saved.'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events').EventEmitter; | |
class MyEmitter extends EventEmitter { | |
constructor(){ | |
super(); | |
let count = 5; | |
let timer = setInterval(() => { | |
this.emit('hi', 'Hello Server!'); | |
count--; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cargar modulo http | |
const http = require('http'); | |
// crear un web server que responde "Hello Server" a todos los requests. | |
const server = http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello Server!'); | |
}); | |
// attachar el servidor al puerto 8000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = [] | |
while (events) { | |
const e = events[0]; // obtener primer elemento de la cola | |
if (e.callback) { // si tiene una funcion asociada, ejecutarla | |
callback(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// blocking | |
const fs = require('fs'); | |
const data = fs.readFileSync('/file.md'); // blocks here until file is read | |
console.log(data); | |
moreWork(); | |
// non-blocking | |
const fs = require('fs'); | |
fs.readFile('/file.md', (err, data) => { |