If you want to send the image as binary data, you can use the built-in function res.sendFile.
If you want this image to be accessible only to the current user, you can use an authorization framework like passportjs or even a high level like huntjs with all things turned on.
Below is an example that checks that req.userexists, and then sends either JSONwith reference to the image or the image itself. If the image is small, it might be better to send base64 .
app.get('/info', function(req,res){
if(req.user){
res.status(200).json({
'imageName':'some image',
'imageUrl':'/someImageUrlOnlyForAuthorizedUsers.jpg'
});
} else {
res.status(401).send('Authorization required!');
}
});
app.get('/someImageUrlOnlyForAuthorizedUsers.jpg', function(req,res){
if(req.user){
res.sendFile('./mySecretImage.jpg');
} else {
res.status(401).send('Authorization required!');
}
});
source
share