Created
December 30, 2018 22:18
-
-
Save andlewis/9f97aa5b0671836842d4038f0f5d9182 to your computer and use it in GitHub Desktop.
Return a random image from a folder
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
const express = require('express'); | |
const recursive = require('recursive-readdir'); | |
const app = express(); | |
const port = 3000; | |
var images = []; | |
app.get('/', (req, res) => { | |
var image = images[Math.floor(Math.random() * images.length)]; | |
res.sendFile(image); | |
}); | |
app.listen(port, () => { | |
getFileList(); | |
console.log('listening on port ' + port) | |
}); | |
getFileList = () => { | |
var path = 'c:/temp'; | |
var regEx = /\.(gif|jpg|jpeg|tiff|png)$/i; | |
recursive(path, (err, files) => { | |
images = files.filter((value, index, array) => { | |
return value.match(regEx); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment