Robin Robin - 5 months ago 35
Node.js Question

image display error using node.js and express.js

Once i open 'localhost:3000' , the image is getting downloaded to my laptop instead of being displayed on the webpage , can anyone please help me out ?

var express= require('express');
var fs = require('fs');
var app= express();

var imgpath= 'C:\Users\Rohit\Downloads\images.jpg';

app.get('/',function(req,res){
res.send(fs.readFileSync(imgpath));
});

app.listen(3000);
console.log('listening');

Answer

You have to set header.

app.get('/',function(req,res){
  res.set('Content-Type', 'image/jpg');//Added line
  res.send(fs.readFileSync(imgpath));
});
Comments