





Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Prepara i tuoi esami
Studia grazie alle numerose risorse presenti su Docsity
Prepara i tuoi esami con i documenti condivisi da studenti come te su Docsity
Trova i documenti specifici per gli esami della tua università
Preparati con lezioni e prove svolte basate sui programmi universitari!
Rispondi a reali domande d’esame e scopri la tua preparazione
Riassumi i tuoi documenti, fagli domande, convertili in quiz e mappe concettuali
Studia con prove svolte, tesine e consigli utili
Togliti ogni dubbio leggendo le risposte alle domande fatte da altri studenti come te
Esplora i documenti più scaricati per gli argomenti di studio più popolari
Ottieni i punti per scaricare
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Breve descrizione di come avviare un server nodeJs
Tipologia: Dispense
1 / 9
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!






var dato = ottieniDatoDaRemoto(url); alert(dato);
ottieniDatoDaRemoto(url, function(dato) { alert(dato); }); Approccio Sincono e Asincrono
var dispatcher = require('../httpdispatcher'); var http = require('http'); dispatcher.setStatic('/resources'); dispatcher.setStaticDirname('static'); dispatcher.onGet("/page1", function(req, res) { res.writeHead( 200 , {'Content-Type': 'text/plain'}); res.end('Page One'); }); dispatcher.onPost("/page2", function(req, res) { res.writeHead( 200 , {'Content-Type': 'text/plain'}); res.end('Page Two'); }); dispatcher.beforeFilter(/ / /, function(req, res, chain) { //any url console.log("Before filter"); chain.next(req, res, chain); }); dispatcher.afterFilter(/ / /, function(req, res, chain) { //any url console.log("After filter"); chain.next(req, res, chain); }); dispatcher.onError(function(req, res) { res.writeHead( 404 ); res.end(); }); http.createServer(function (req, res) { dispatcher.dispatch(req, res); }).listen( 1337 , '127.0.0.1');
var dispatcher = require('httpdispatcher'); dispatcher.onGet("/page1", function(req, res) { res.writeHead( 200 , {'Content-Type': 'text/plain'}); res.end('Page One'); }); dispatcher.onPost("/page2", function(req, res) { res.writeHead( 200 , {'Content-Type': 'text/plain'}); res.end('Page Two'); }); http.createServer(function (req, res) { dispatcher.dispatch(req, res); }).listen( 1337 , '127.0.0.1');
var HttpDispatcher = function() { this.listeners = { get: [ ], post: [ ] }; this.errorListener = function() { } this.staticFolderPrefix = '/static'; } HttpDispatcher.prototype.setStatic = function(folder) { this.staticFolderPrefix = folder; } HttpDispatcher.prototype.staticListener = function(req, res) { var url = require('url').parse(req.url, true); var filename = require('path').join(".", url.pathname); var errorListener = this.errorListener; require('fs').readFile(filename, function(err, file) { if (err) { errorListener(req, res); return; } res.writeHeader( 200 , { "Content-Type": require('mime').lookup(filename) }); res.write(file, 'binary'); res.end(); }); } HttpDispatcher.prototype.dispatch = function(req, res) { var parsedUrl = require('url').parse(req.url, true); if (parsedUrl.pathname.indexOf(this.staticFolderPrefix) == 0) { this.staticListener(req, res); return; } var method = req.method.toLowerCase(); if (method == 'post') { var body = '';
var url_parts = require('url').parse(req.url, true); console.log(url_parts.query); // stamperà una mappa chiave/valore dei parametri
var body = ''; req.on('data', function(data) { body += data; }); req.on('end', function() { var post = require('querystring').parse(body); console.log(post); });
var mysql = require('db-mysql'); new mysql.Database({ hostname: 'localhost', user: 'utentedb', password: 'passwdb', database: 'testdb' }).connect(function(error) { if(error) return console.log("Connection error"); this.query().select("*") .from("libri") .where("letto = ?", [true]) .order({ titolo: true }) .execute(function(error, rows, cols) { if (error) return console.log("Query error"); for (var i in rows) console.log(rows[i]); }); });