sashomaga

node.js sample server

Mar 3rd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require("http");
  2. var url = require("url");
  3. var fs = require('fs');
  4.  
  5. function start(route) {
  6.     function onRequest(request, response) {
  7.         var pathname = url.parse(request.url).pathname; //url directory
  8.         var param = url.parse(request.url).query; //get parameters  
  9.         console.log(pathname);
  10.         //route(pathname);
  11.         switch (pathname) {          
  12.             case '/css.css':
  13.                 fs.readFile('./css.css', function (err, css) {
  14.                     if (err) {
  15.                         throw err;
  16.                     }
  17.                     response.writeHeader(200, { "Content-Type": "text/css" });
  18.                     response.write(css); //send css
  19.                     response.end();
  20.                 });
  21.                 break;
  22.             case '/' :
  23.                 fs.readFile('./index.html', function (err, html) {
  24.                     if (err) {
  25.                         throw err;
  26.                     }
  27.                     response.writeHeader(200, { "Content-Type": "text/html" });
  28.                     response.write(html); //send html
  29.                     response.end();
  30.                 });
  31.                 break;
  32.        }        
  33.     }
  34.     http.createServer(onRequest).listen(8888);
  35.     console.log("Server has started.");
  36. }
  37. function route(pathname) {    
  38. }
  39. start(route); //start server
Advertisement
Add Comment
Please, Sign In to add comment