Skip to content

Instantly share code, notes, and snippets.

@stuartjnelson
Created March 20, 2020 19:10
Show Gist options
  • Save stuartjnelson/294033b10ac06e73cf42658694adb8f4 to your computer and use it in GitHub Desktop.
Save stuartjnelson/294033b10ac06e73cf42658694adb8f4 to your computer and use it in GitHub Desktop.
Node check for images
// 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