Created
June 6, 2016 06:40
-
-
Save guptasanchit90/6dfc4022232efaf6a044dd329476f7c4 to your computer and use it in GitHub Desktop.
Simple Image hosting from file system in Node.js using express routes
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
var express = require('express'); | |
var app = express(); | |
var fs = require('fs'); | |
app.get('/get', function (req, res) { | |
res.sendFile(__dirname + '/' + req.query.type + '/' + req.query.name); | |
}) | |
app.get('/list', function (req, res) { | |
fs.readdir(__dirname + '/' + req.query.type, function(err, items) { | |
var output = '{"Files":' + JSON.stringify(items) + '}'; | |
res.send(output); | |
}); | |
}) | |
var server = app.listen(8081, function () { | |
var host = server.address().address | |
var port = server.address().port | |
console.log("Example app listening at http://%s:%s", host, port) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment