I am trying to create a module for routing in Node.js inside a folder in the main server, like this.
+root.js (server main)
+modulos (folder where rutas.js is)
++rutas.js
+shalimar (folder where all the static files are)
++muestrame.html (I want to send this)
My code looks like:
root.js (main server)
var express = require('express');
var router_app = require('./modulos/rutas');
web.listen(4200, function() {
console.log('Servidor Web Iniciado en el Puerto : 4200');
});
web.use(express.static(__dirname + '/shalimar'));
web.use('/app', router_app);
I am trying to create a module for routing in Node.js inside a folder in the main server, like this.
+root.js (server main)
+modulos (folder where rutas.js is)
++rutas.js
+shalimar (folder where all the static files are)
++muestrame.html (I want to send this)
My code looks like:
root.js (main server)
var express = require('express');
var router_app = require('./modulos/rutas');
web.listen(4200, function() {
console.log('Servidor Web Iniciado en el Puerto : 4200');
});
web.use(express.static(__dirname + '/shalimar'));
web.use('/app', router_app);
Here I call rutas.js inside modulos
rutas.js (inside modulos)
var express = require('express');
var router = express.Router();
router.get('/', (entra, sale) => {
console.log('Cargando desde routing.js');
sale.sendFile('/shalimar/muestrame.html', {root : __dirname});
});
This error gets thrown:
Error: ENOENT: no such file or directory, stat 'C:\Users\Eleazar Ortega\desktop\uneweb\modulos\shalimar\muestrame.html'
My question is how to level up (outside modulos) in the folder to access shalimar/muestrame.html
- If the source file is not in the project, maybe you have to specify his path gloabally given the hierarchy of your file system, rather than using a relative path – edkeveked Commented Nov 23, 2017 at 15:19
1 Answer
Reset to default 4Well yes, __dirname
is the value of the current folder, so it is still relative to modulos
. You'd have to store that value to a variable and pass it through from the index file in the root.
Why not just do '../shalimar/muestrame.html'
?
Or even better path.resolve('../shalimar/muestrame.html')