marcel
marcel

Reputation: 3272

NodeJS send picture to webpage

Id like to get pictures from a nodejs express-server to show on my webpage. I have a relative path to the file on the server. This path also includes subfolder.

 <img src="pictures/?path=rel_path"/>

On the serverside I tried this:

app.get('/pictures', function(req, res){
    res.sendfile(__dirname + res.query.path);
});

I get this error:

Error: ENOENT, stat d:\...\src.\rel_path        // where comes src. from?   

Unfortunately it doesn't work, because the file cannot be found. Is this necessary at all, or is there a better way to send the files to the webpage?

Upvotes: 0

Views: 1175

Answers (1)

Diya Khan
Diya Khan

Reputation: 201

To serve any static content from your server (either images or other files) you can place this code in your app.js file

// static files
app.use(express.static(path.join(__dirname, '/public')));

No need to define route for this.

Upvotes: 2

Related Questions