basic problem in node js with the routes

1

Hello friends I'm starting to see in node js and I get this error:

root@sommer0123-AO532h:/home/sommer0123/Escritorio/Nodejs/estatica# node index.js
module.js:471
    throw err;
    ^

Error: Cannot find module './modulos/server'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object. (/home/sommer0123/Escritorio/Nodejs/estatica/index.js:4:16)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
root@sommer0123-AO532h:/home/sommer0123/Escritorio/Nodejs/estatica# 

The problem as I understand it is the route but the route is fine.

Sorry for the touchpad.

my code:

index.js

var http = require('http');
var url = require('url');
var fs = require('fs');
var servidor = require('./modulos/server');
servidor.crear(http, url, fs);
console.log('El servidor esta funcionando correctamente en http://localhost:3000/');

server.js

var mine_types = {
   'js' : 'text/javascript',
   'html' : 'text/html',
   'css' : 'text/css',
   'jpg' : 'image/jpg',
   'gif' : 'image/gif',
   'png' : 'image/png'
};
function crear(http, url, fs){
   http.createServer(function(peticion, respuesta){
      var ruta_a_archivo = devolverRutaArchivo(url, peticion);
      leerArchivo(fs, ruta_a_archivo, function(numero, contenido_archivo){
         if(numero === 404){
            respuesta.writeHead(numero, 'text/plain');
            respuesta.end('Error 404. El enlace no existe o ha dejado de existir.');
         }else if(numero === 500){
            respuesta.writeHead(numero, 'text/plain');
            respuesta.end('Error interno.');
         }else{
            var extension = ruta_a_archivo.split('.').pop();
            var mine_type = mine_types[extension];
            respuesta.writeHead(numero, {'Content-Type': mine_type});
            respuesta.end(contenido_archivo);
         }
      })
   }).listen(3000, '127.0.0.1');
}
function devolverRutaArchivo(url, peticion){
   var path_nombre = (url.parse(peticion.url).pathname == '/') ? '/index.html' : url.parse(peticion.url).pathname;
   var ruta_a_archivo = 'contenido/' + path_nombre;
   return ruta_a_archivo;
}
function leerArchivo(fs, ruta_a_archivo, callback){
   fs.exists(ruta_a_archivo, function(existe){
      if(existe){
         fs.readFile(ruta_a_archivo, function(error, contenido_archivo){
            if(error){
               callback(500, null);
            }else{
               callback(200, contenido_archivo);
            }
         });
      }else{
         callback(404, null);
      }
   });
}
exports.crear = crear;

animales.js

var lista = {
   'aves': new Array('Loro' , 'Canario'),
   'mamiferos': new Array('Perro' , 'Caballo' , 'Tigre'),
   'reptiles': new Array('Cocodrilo', 'Tortuga', 'Iguana')
};
function dibujarCodigoHtml(grupo){
   var html = '';
   html += '';
   html += '';
   html += '';
   html += '';
   html += '';
   html += '';
   html += ' Seleccione el tipo de animal: ';
   html += ' ' + listarGrupos(grupo) + ' ';
   html += '';
   html += '';
   html += listarAnimales(grupo);
   html += '';
   html += '';
   return html;
}
function listarGrupos(grupo){
   var html = ' --- ';
   var selected;
   for (var item in lista) {
      selected = (item == grupo) ? 'selected="selected"' : '';
      html += ' ' + item + ' ';
   }
   return html;
}
function listarAnimales(grupo){
   var html = '';
   if(lista[grupo] != undefined){
      html += '
    ';       for (var i in list [group]) {          html + = '
  • ' + list [group] [i] + '
  • ';       }       html + = '
';    }    return html; } exports.drawCodigoHtml = drawCodeHtml;

index.html

<!DOCTYPE>
<html>
<head>
   <title> Ejemplo de Node.js </title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <link type="text/css" href="/css/style.css" rel="stylesheet" />
</head>
<body>
   <h1> Index </h1>
   <ul>
      <li> <a href="/index.html"> Index </a> </li>
      <li> <a href="/info.html"> Info </a> </li>
   </ul>
   <p> Bienvenido/a a la página principal </p>
</body>
</html>
info.html

body {
   background-color: #D4ECF8;
}
<!DOCTYPE>
<html>
<head>
   <title> Ejemplo de Node.js </title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
   <h1> Info </h1>
   <ul>
      <li> <a href="/index.html"> Index </a> </li>
      <li> <a href="/info.html"> Info </a> </li>
   </ul>
   <p> Esta es la página info </p>
</body>
</html>

origin of the code with which I am using: link

Error img:

    
asked by simon 17.02.2017 в 03:34
source

2 answers

1

index.js is in the wrong directory, you must upload it to the "static" folder, from there, NodeJS will find without problems './modulos/server'.

    
answered by 23.02.2017 / 15:05
source
0

You must use the full path "./contenido/modulos/server". I also recommend that you have the file to create the server in the base folder of the project. And if not, at least the index.js that reference it. This for reasons of having the project structured, for this type of structure. Project

  • App.js (the server)
  • nodejs module folder (for the modules you use, it is generated alone)
  • views (here's the html)
  • public (here a static folder)

I also recommend looking for express since it structures the project in a great way.

    
answered by 23.02.2017 в 15:57