I have 51 images in a folder how do I display all the images serially? the code is given below.
var i = "hamarabajaj_";
var p = 0;
var q = 0;
var r = ".png";
var http = require('http'),
fs = require('fs');
p = p+1;
q = i+p+r;
fs.readFile('/home/bidya271/Documents/Health_Monitor/pictures/'+q, function(err, data) {
if (err) throw err;
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end(data); // Send the file data to the browser.
}).listen(8124);
console.log('Server running at http://localhost:8124/');
});
I'm not entirely sure what your goal is, but it is impossible to "serially" send images from a HTTP server to a client. However, you can make a make a server that takes a request from a client and returns images on demand.
Below is an example of a server that serves up images based upon an image prefix:
var express = require("express"),
app = express(),
imageDir = __dirname + "/pictures/",
imageSuffix = "-image.png",
fs = require("fs");
app.get("/images/:id", function (request, response) {
var path = imageDir + request.params.id + imageSuffix;
console.log("fetching image: ", path);
response.sendFile(path);
});
app.listen(8080);
The above code uses the Express to easily bootstrap a web server; you can install it with npm install express
.
Assuming that you add pictures to the "pictures" folder, you can request images in you browser by hitting the following URL:
http://localhost:8080/images/0
which would return the image at pictures/0-image.png
. /1
would return 1-image.png
and so on.