What is Node.js?

What is npm?

Node.js interactive

        
> 1+1
2
> 4*10
40
> a = require("http")
{ parsers: 
   { name: 'parsers',
     constructor: [Function],
     max: 1000,
     list: [] },
  STATUS_CODES: 
   { '100': 'Continue',
     '101': 'Switching Protocols',
     '102': 'Processing',
     '200': 'OK',
     ...
  get: [Function],
  Server: { [Function: Server] super_: { [Function: Server] super_: [Object] } },
  createServer: [Function],
  _connectionListener: [Function: connectionListener],
  Client: [Function: deprecated],
  createClient: [Function: deprecated] }
        
      

HTTP Server


var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
}).listen(3000);

      
Quelle

Express


var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log('Example app listening at http://%s:%s', host, port)

      
Quelle

node Websocket

        
var ws = require("nodejs-websocket")
var server = ws.createServer(function (conn) {
  console.log("New connection")
  conn.on("text", function (str) {
    console.log("Received "+str)
    conn.sendText(str.toUpperCase()+"!!!")
  })
  conn.on("close", function (code, reason) {
    console.log("Connection closed")
  })
}).listen(8001)
        
      
Quelle

Chat :: package.json

        
{
  "name": "Chat",
  "description": "Ein einfacher Chat mit Node.js",
  "author": "Dein Name",
  "dependencies" : {
    "express": "3.x",
    "socket.io": "*"
  }
}
        
      
Quelle

Chat :: server.js

        
var express = require('express'), app = express(), server = require('http').createServer(app), io = require('socket.io').listen(server), conf = require('./config.json');

server.listen(conf.port);
app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection', function (socket) {
  socket.emit('chat', { zeit: new Date(), text: 'Du bist nun mit dem Server verbunden!' });
  socket.on('chat', function (data) {
    io.sockets.emit('chat', { zeit: new Date(), name: data.name || 'Anonym', text: data.text });
  });
});
console.log('Der Server läuft nun unter http://127.0.0.1:' + conf.port + '/');
        
      
Quelle

Chat :: client.js

        
$(document).ready(function(){
  var socket = io.connect();
  socket.on('chat', function (data) {
    var zeit = new Date(data.zeit);
    $('#content').append(
      $('<li></li>').append(
      $('<span>').text('[' +
        (zeit.getHours() < 10 ? '0' + zeit.getHours() : zeit.getHours())
        + ':' +
        (zeit.getMinutes() < 10 ? '0' + zeit.getMinutes() : zeit.getMinutes())
        + '] '),
        $('<b>').text(typeof(data.name) != 'undefined' ? data.name + ': ' : ''),
        $('<span>').text(data.text))
      );
      $('body').scrollTop($('body')[0].scrollHeight);
    });
    function senden(){
      var name = $('#name').val();
      var text = $('#text').val();
      socket.emit('chat', { name: name, text: text });
      $('#text').val('');
    }
    $('#senden').click(senden);
    $('#text').keypress(function (e) {
      if (e.which == 13) {
        senden();
      }
    });
});
        
      
Quelle

Chat :: index.html


<!DOCTYPE html>
<html>
  <head>
    <title>Node.js Chat</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" />
    <script src="socket.io/socket.io.js"></script>
    <script src="jquery.min.js"></script>
    <script src="client.js"></script>
  </head>
  <body>
    <header>Node.js Chat</header>
    <ul id="content"></ul>
    <footer>
      <input id="name" type="text" placeholder="dein Name" />
      <input id="text" type="text" placeholder="schreibe etwas..." />
      <input id="senden" type="submit" class="btn" value="senden" />
    </footer>
  </body>
</html>

      
Quelle

Danke

/