Hello friends, I am starting to see in node js and I get this error:
root@sommer0123-AO532h:/home/sommer0123/Desktop/Nodejs/static# node index.js module.js:471 throw error; ^ Error: Cannot find module './modules/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/Desktop/Nodejs/static/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/Desktop/Nodejs/static#
the problem as I understand is the route but the route is fine.
my code:
index.js
var http = require('http'); var url = require('url'); var fs = require('fs'); var server = require('./modules/server'); server.create(http, url, fs); console.log('The server is running correctly at 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 create(http, url, fs){ http.createServer(function(request, response){ var path_to_file = returnPathFile(url, request); readFile(fs, path_to_file, function(number, file_content){ if(number === 404){ response.writeHead(number, 'text/plain'); response.end('Error 404. The link does not exist or has ceased to exist.'); }else if(number === 500){ response.writeHead(number, 'text/plain'); response.end('Internal error.'); }else{ var extension = path_to_file.split('.').pop(); var mine_type = mine_types[extension]; response.writeHead(number, {'Content-Type': mine_type}); response.end(file_content); } }) }).listen(3000, '127.0.0.1'); } function returnFilePath(url, request){ var path_name = (url.parse(request.url).pathname == '/') ? '/index.html' : url.parse(request.url).pathname; var path_to_file = 'content/' + path_name; return path_to_file; } function readFile(fs, path_to_file, callback){ fs.exists(path_to_file, function(exists){ if(exists){ fs.readFile(path_to_file, function(error, file_content){ if(error){ callback(500, null); }else{ callback(200, file_content); } }); }else{ callback(404, null); } }); } exports.create = create;
animals.js
var list = { 'birds': new Array('Parrot' , 'Canary'), 'mammals': new Array('Dog' , 'Horse' , 'Tiger'), 'reptiles': new Array('Crocodile', 'Turtle', 'Iguana') }; function drawHtmlCode(group){ var html = ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ' Select the type of animal: '; html += ' ' + listGroups(group) + ' '; html += ''; html += ''; html += listAnimals(group); html += ''; html += ''; returnhtml; } function listGroups(group){ var html = ' --- '; var selected; for (var item in list) { selected = (item == group) ? 'selected="selected"' : ''; html += ' ' + item + ' '; } returnhtml; } function listAnimals(group){ var html = ''; if(list[group] != undefined){ html += '
- ';
for (var i in list[group]) {
html += '
- ' + list[group][i] + ' '; } html += '
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>
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: http://fernando-gaitan.com.ar/introduccion-a-node-js-parte-3-crear-modulos/
img of the error:
index.js is in the wrong directory, you should upload it to the "static" folder, from there NodeJS will find './modules/server' without any problem.
You must use the full path "./content/modules/server". I also recommend that you have the file to create the server in the base folder of the project. And if not, then at least the index.js that references it. This for reasons of having the project structured, for this type of structure. Project
I also recommend looking into express as it structures the project in a great way.