AntonioVillanueva

Servidor en Node Js

Sep 24th, 2025
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. index.js
  2. var server = require ("./server");
  3. var router = require ("./routers");
  4. var requestHandlers = require ("./requestHandlers");
  5.  
  6. var handle ={}
  7. handle ["/"] = requestHandlers.raiz;
  8. handle ["/iniciar"] = requestHandlers.iniciar;
  9. handle ["/busca"] = requestHandlers.busca;
  10. handle ["/subir"] = requestHandlers.subir;
  11.  
  12. server.inicio (router.route,handle);
  13.  
  14.  
  15. ****************************************************************************************
  16.  
  17. server.js
  18.  
  19. var http=require ("http");
  20. var url =require ("url");
  21.  
  22. function inicio (route,handle){
  23.     function onRequest (request, response){
  24.         var pathname =url.parse(request.url).pathname;
  25.         console.log("Peticion para "+pathname+ " recibida.");
  26.  
  27.         route (handle,pathname,response);
  28.     }
  29.     http.createServer(onRequest).listen (8888);
  30.     console.log ("Server Running");
  31. }
  32.  
  33. exports.inicio=inicio;
  34.  
  35. ****************************************************************************************
  36.  
  37. routers.js
  38. function route (handle,pathname,response){
  39.  
  40.     console.log ("Rutear peticion a "+pathname );
  41.  
  42.     if (typeof handle[pathname] === 'function'){
  43.         //return handle  [pathname]();
  44.                   handle  [pathname](response);
  45.  
  46.     } else{
  47.         console.log ("No hay manipulador para "+pathname);
  48.         response.writeHead (404,{"Content-Type":"text/html"});
  49.         response.write("404 no encontado");
  50.         response.end()
  51.         //return "404 no encontrado";
  52.     }
  53.  
  54. }
  55. exports.route =route;
  56.  
  57.  
  58. ****************************************************************************************
  59.  
  60.  
  61. requestHandlers.js
  62.  
  63. var exec =require ("child_process").exec;
  64.  
  65. function raiz(response){
  66.     console.log ("Manip. raiz / ");
  67.     html (response,"raiz");
  68.     //return "raiz /";
  69. }
  70.  
  71. function iniciar (response){
  72.     console.log ("Manip. iniciar ");
  73.  
  74.     exec ("ls -lah", function (error, stdout,stderr) {
  75.         html (response,stdout);
  76.     }); ;
  77. }
  78.  
  79. function busca (response){
  80.     console.log ("Manip.busca");
  81.     exec ("find /",
  82.     {timeout:10000,maxBuffer:20000*1024},
  83.     function (error,stdout, stderr){
  84.         html(response,stdout);     
  85.     });
  86. }
  87.  
  88. function subir (response){
  89.     console.log ("Manip. subir ");
  90.     html (response,"sube");
  91.     //return "Hola subir";
  92. }
  93.  
  94. //Funciones auxiliares
  95. function html(response,msg){
  96.     response.writeHead ( 200 , {"Content-Type": "text/html"});
  97.     response.write (msg);
  98.     response.end();
  99. }
  100.  
  101. exports.raiz=raiz;
  102. exports.iniciar =iniciar;
  103. exports.busca=busca;
  104. exports.subir =subir;
  105.  
  106.  
  107.  
  108.  
Tags: Node Js
Advertisement
Add Comment
Please, Sign In to add comment