最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access a file outside the current folder in Node.js - Stack Overflow

programmeradmin2浏览0评论

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

Share Improve this question edited Dec 11, 2020 at 5:38 Evan93 3512 silver badges16 bronze badges asked Nov 23, 2017 at 15:17 Eleazar OrtegaEleazar Ortega 5392 gold badges7 silver badges19 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 4

Well 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')

发布评论

评论列表(0)

  1. 暂无评论