Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active May 11, 2017 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/7bdd2fd38ccdc4efbdb2cb785a41ba54 to your computer and use it in GitHub Desktop.
Save juanpabloaj/7bdd2fd38ccdc4efbdb2cb785a41ba54 to your computer and use it in GitHub Desktop.
20170511 hello nodejs

class: center, middle

Hello nodejs

Algunos ejemplos de nodejs

Juan Pablo Abarzúa

2017 05 11


Nodejs

"Node.js es un entorno de ejecución para JavaScript construido con el motor de JavaScript V8 de Chrome. … . El ecosistema de paquetes de Node.js, npm, es el ecosistema mas grande de librerías de código abierto en el mundo."

https://nodejs.org/es/


Instalación

Documentación oficial con gestionadores de paquetes

https://nodejs.org/en/download/package-manager


nvm: node version manager

https://github.com/creationix/nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

nvm install v4.2

class: center, middle

nodejs: "Las baterías no están incluidas"


npm: node package manager

Gestionandor de paquetes de nodejs

https://www.npmjs.com/


Servidor web con express

https://expressjs.com/

# crear archivo package.json
npm init
# instalar express
npm install --save express

// index.js
var express = require('express');
var app = express();

app.get('/user/:id', function (req, res) {
  console.log(req.params);
  console.log(req.query);
  var message = 'Hello ' + req.params.id;
  res.send(message);
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Cliente

$ curl "localhost:3000/user/alice?today=monday&tomorrow=tuesday"
Hello alice

Servidor

$ node index.js
Example app listening on port 3000!
{ id: 'alice' }
{ today: 'monday', tomorrow: 'tuesday' }

Servidor de websockets

https://github.com/websockets/ws

npm install --save ws


Servidor

// index.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({port:8080});

wss.on('connection', function connection (ws) {

  console.log('Started connection with browser');

  ws.on('message', function (message) {
    console.log('From Browser: %s', message);
  });

  let i = 0;
  setInterval(function() {
    console.log('Sending to browser ' + ++i);
    ws.send('the value es ' + i);
  }, 1000);

});

Cliente

<html>
    <head>
        <script src="client.js" type="text/javascript" charset="utf-8"></script>
    </head>
</html>
// client.js
var ws = new WebSocket('ws://localhost:8080');

ws.onopen = function (event) {
  ws.send('Hello from browser');
};

ws.onmessage = function (event) {
  console.log('From Server: %s', event.data);
};

Servidor

$ node index.js
Started connection with browser
From Browser: Hello from browser
Sending to browser 1
Sending to browser 2

Browser

From Server: the value es 1
From Server: the value es 2

Miembros

https://nodejs.org/en/foundation/members/#corporate-members


class: center, middle

Preguntas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment