简体   繁体   中英

Get Uploaded Files from NodeJs

I'm trying to retrieve files uploaded to the server with no luck. I'm getting instead: 403 - forbidden error. (I'm fairly new to NodeJs)

This is NodeJS code:

// Get Thumbnail Images
router.param('imageName', function(req, res, next, imageName) {
    req.imageURL = "../uploads/thumbnails/" + imageName;
    return next();
});

router.get('/tn/:imageName', function(req, res) {
    var options = {
        root: __dirname
    };
    res.sendFile(req.imageURL, options);
});

this is HTML code:

<img ng-src="/tn/{{ card.backImg }}" class="" alt=""/>

In addition I tried to add this line in my app.js:

app.use(express.static(path.join(__dirname, 'uploads')));

I'm not sure what are the best practices for such situation (all users access to uploaded images). So would you please provide a general explanation as well about public folders, static and best practices.

Thank you in advance.

I think that the source code below is able to work with your case:

app.get('/tn/:imageName', function(req ,res) {
    var path = require('path');
    var file = path.join(__dirname, "/../uploads/thumbnails/" , req.params.imageName);
    res.sendFile(file);
});

Note: I am using ExpressJS 4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM