example/server.js
function onRequest(request, response) {
  var filename = path.join(__dirname, request.url);

  // Serving server.js from cache. Useful for microbenchmarks.
  if (request.url === cachedUrl) {
    if (response.push) {
      // Also push down the client js, since it's possible if the requester wants
      // one, they want both.
      var push = response.push('/client.js');
      push.writeHead(200);
      fs.createReadStream(path.join(__dirname, '/client.js')).pipe(push);
    }
    response.end(cachedFile);
  }

  // Reading file from disk if it exists and is safe.
  else if ((filename.indexOf(__dirname) === 0) && fs.existsSync(filename) && fs.statSync(filename).isFile()) {
    response.writeHead(200);
    var fileStream = fs.createReadStream(filename);
    fileStream.pipe(response);
    fileStream.on('finish',response.end);
  }

  // Example for testing large (boundary-sized) frames.
  else if (request.url === "/largeframe") {
    response.writeHead(200);
    var body = 'a';
    for (var i = 0; i < 14; i++) {
      body += body;
    }
    body = body + 'a';
    response.end(body);
  }

  // Otherwise responding with 404.
  else {
    response.writeHead(404);
    response.end();
  }
}
function onRequest(request, response) {
  var filename = path.join(__dirname, request.url);

  // Serving server.js from cache. Useful for microbenchmarks.
  if (request.url === cachedUrl) {
    if (response.push) {
      // Also push down the client js, since it's possible if the requester wants
      // one, they want both.
      var push = response.push('/client.js');
      push.writeHead(200);
      fs.createReadStream(path.join(__dirname, '/client.js')).pipe(push);
    }
    response.end(cachedFile);
  }

  // Reading file from disk if it exists and is safe.
  else if ((filename.indexOf(__dirname) === 0) && fs.existsSync(filename) && fs.statSync(filename).isFile()) {
    response.writeHead(200);
    var fileStream = fs.createReadStream(filename);
    fileStream.pipe(response);
    fileStream.on('finish',response.end);
  }

  // Example for testing large (boundary-sized) frames.
  else if (request.url === "/largeframe") {
    response.writeHead(200);
    var body = 'a';
    for (var i = 0; i < 14; i++) {
      body += body;
    }
    body = body + 'a';
    response.end(body);
  }

  // Otherwise responding with 404.
  else {
    response.writeHead(404);
    response.end();
  }
}
function onRequest ( request , response ) { var filename = path . join ( __dirname , request . url ) ; // Serving server.js from cache. Useful for microbenchmarks. if ( request . url === cachedUrl ) { if ( response . push ) { // Also push down the client js, since it's possible if the requester wants // one, they want both. var push = response . push ( '/client.js' ) ; push . writeHead ( 200 ) ; fs . createReadStream ( path . join ( __dirname , '/client.js' ) ) . pipe ( push ) ; } response . end ( cachedFile ) ; } // Reading file from disk if it exists and is safe. else if ( ( filename . indexOf ( __dirname ) === 0 ) && fs . existsSync ( filename ) && fs . statSync ( filename ) . isFile ( ) ) { response . writeHead ( 200 ) ; var fileStream = fs . createReadStream ( filename ) ; fileStream . pipe ( response ) ; fileStream . on ( 'finish' , response . end ) ; } // Example for testing large (boundary-sized) frames. else if ( request . url === "/largeframe" ) { response . writeHead ( 200 ) ; var body = 'a' ; for ( var i = 0 ; i < 14 ; i ++ ) { body += body ; } body = body + 'a' ; response . end ( body ) ; } // Otherwise responding with 404. else { response . writeHead ( 404 ) ; response . end ( ) ; } }
The callback to handle requests
The callback to handle requests