Reputation: 78334
I am trying to load an image file in node.js. The file will be in ram and loaded outside of the main loop. Its just a 1x1 pixel.
How do I open an image file?
I tried the below:
var fs = require('fs');
fs.readFile('/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/pixel1x1.gif', function(err,pixel){
if(err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
console.log(pixel);
});
Server running at http://127.0.0.1:8124/
<Buffer 47 49 46 38 39 61 01 00 01 00 80 00 00 00 00 00 ff ff ff 21 f9 04 01 00 00 00 00 2c 00 00 00 00 01 00 01 00 00 02 01 44 00 3b>
Image could not be display because it contains errors.
res.writeHead(200, {'Content-Type': 'image/gif'});
res.end(pixel);
Upvotes: 3
Views: 1466
Reputation: 31
If it is just a 1x1 gif image then you don't need to store image file and load it, because there is a good NPM package blankgif that already has blank 1x1 gif loaded with base64 string right in the code, so it will work faster then loading image separately. Also, there is some helper function that can help you to send this image in expressjs.
To use this package you can just use this code:
var blankgif = require('blankgif');
var express = require('express');
var app = express();
app.get('/track.gif', blankgif.sendBlankGif);
app.listen(3000, function () {
console.log('App listening on port 3000!');
});
I also assume that you want to implement this because you need to track visitors somewhere with this blank image. So you can do it in two ways with this library:
1) Use special middleware of blankgif that adds additional function to your response object
var blankgif = require('blankgif');
var express = require('express');
var app = express();
app.use(blankgif.middleware());
app.get('/track.gif', function(req, res) {
console.log('request information logged');
res.set('Cache-Control', 'public, max-age=0');
res.status(200).sendBlankGif();
});
app.listen(3000, function () {
console.log('App listening on port 3000!');
});
2) Just use custom middleware before blankgif.sendBlankGif
var blankgif = require('blankgif');
var express = require('express');
var app = express();
app.get('/track.gif', function(req, res, next) {
console.log('request information logged');
next();
}, blankgif.sendBlankGif);
app.listen(3000, function () {
console.log('App listening on port 3000!');
});
Also if you don't want to use this library for some reasons (or if you want another gif image) you can serve this image manually:
var express = require('express');
var app = express();
var fs = require('fs');
var path = require('path');
var buff = null;
fs.readFile(path.resolve(__dirname, 'pixel1x1.gif'), function (err, pixel) {
if (err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
buff = pixel;
});
app.get('/track.gif', function (req, res) {
console.log('request information logged');
res.type('image/gif');
res.send(buff);
});
app.listen(3000, function () {
console.log('App listening on port 3000!');
});
Upvotes: 3