expressjs: Sending a file from parent directory expressjs: Sending a file from parent directory express express

expressjs: Sending a file from parent directory


You have to mention root as the second parameter of sendfile().

For example:

app.get('/:dir/:file', function(req, res) {  var dir = req.params.dir,      file = req.params.file;  res.sendfile(dir + '/' + file, {'root': '../'});});

You can find more details here:https://github.com/visionmedia/express/issues/1465


You need to use express.static.

Say you have the following directory set up:

/app   /buried       /deep           server.js   /public       index.html

Then you should have the following Express configuration:

var express = require('express');var server = express.createServer();server.configure(function(){    server.use(express.static(__dirname + '../../public'));});server.listen(3000);

res.sendfile is meant for "finer-grain" transferring of files to the client. See API docs for example.


parent folder: -app -routes.js -index.htmlIn the above case, Add the following code to routes.js to send a file from parent directory.

var path=require("path") //assuming express is installed app.get('/', function(req, res){res.sendFile(path.join(__dirname + '/../index.html'));});