javascript - Express parent folder access -


hi guys working on express aplication. in top of express file wrote line since static files located in working directory:

app.use(express.static(__dirname));  

now want send file exist in parent folder of current folder:

app.get('/test', function(req, res) { res.sendfile("../../test.html"); }); 

it didn't work me, because static files must exist in directory defind above, make exception , make code work?

express.static , res.sendfile don't know each other. happen share lot of same internals aren't related.

you can put test.html wherever want , reference using node's built-in path module. example, if file structure looks this:

test.html real-app/ ├── app.js ├── node_modules/ └── package.json 

then can send test.html this:

var path = require('path');  // ...  app.get('/test', function(req, res) {   var testhtmlpath = path.resolve(__dirname, '..', '..', 'test.html');   res.sendfile(testhtmlpath); }); 

ps: wouldn't recommend way you're sending static files. serving files same directory app code (which __dirname means) can cause code disclosure, hackers can use exploit problems in code. example, if hacker visited url:

http://yourapp.com/app.js

they able see app.js, has of application's code. don't want reveal hacker! navigate routes /secret-passwords.json or other similar files.

typically, static files placed special directory, called static or public. can serve files directory this:

var path = require('path');  // ...  var staticfilespath = path.resolve(__dirname, 'public'); app.use(express.static(staticfilespath)); 

in general, should pretty careful sending files live outside of app's code.

hope helps!


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -