Skip to content

Instantly share code, notes, and snippets.

@andlewis
Created December 30, 2018 22:18
Show Gist options
  • Save andlewis/9f97aa5b0671836842d4038f0f5d9182 to your computer and use it in GitHub Desktop.
Save andlewis/9f97aa5b0671836842d4038f0f5d9182 to your computer and use it in GitHub Desktop.
Return a random image from a folder
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