Created
March 20, 2020 19:10
-
-
Save stuartjnelson/294033b10ac06e73cf42658694adb8f4 to your computer and use it in GitHub Desktop.
Node check for images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Currenet setup | |
.use('/images/', express.static( path.resolve(`${imagePath}/images/`)) ) | |
// New idea | |
.get('/' + 'images' + '/*', function (req, res) { | |
const imgUrl = `${imagePath}${req.url}` | |
console.log(`imgUrl: ${imgUrl}`) | |
const urlExists = url => new Promise( | |
(resolve, reject) => request.head(url) .on("response", res => resolve(res.statusCode.toString()[0] ==="2")) | |
) | |
urlExists(`${imagePath}${req.url}`) | |
.then(exists => { | |
return exists ? `${imagePath}${req.url}` : false | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment