Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display images in HTML + Nodejs

I am new to nodejs. I want to create a very simple website which has 3 pages. In every page I want to display an image to make the pages look uniform.

My code looks like this:

    /**
     * Module dependencies.
     */

    var express = require('express');
    var routes = require('./routes');
    var user = require('./routes/user');
    var http = require('http');
    var path = require('path');
    var fs = require('fs');
    var mail = require("nodemailer").mail;

/*List of variables*/

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.get('/main', function(req, res) {

    fs.readFile('./home.html', function(error, content) {
        if (error) {
            res.writeHead(500);
            res.end();
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end(content, 'utf-8');
        }
    });

});

/* After this I have code for post event - all these navigation works perfectly fine*/

in home.html file I have an image to display:

/*Header part of HTML file*/

<body>

        <img class="logo" src="./Heading.png" alt="My_Logo">
        console.log (src);
        <h1 class="center">Welcome to message reprocessing</h1>
    </br>
</body>

This image is not displayed in my browser. When I check my console I have this error:

GET http://localhost:3000/Heading.png 404 (Not Found) 

Please help, thanks

like image 332
DivB Avatar asked Jan 20 '14 13:01

DivB


People also ask

What is node-HTML-to-image?

That is why I created node-html-to-image. A Node.js module that generates images from HTML. Here is the simplest example, you provide an output path and a HTML string. That’s all! .then(() => console.log('The image was created successfully!'))

How to generate images from HTML in Node JS?

A Node.js module that generates images from HTML. Here is the simplest example, you provide an output path and a HTML string. That’s all! .then(() => console.log('The image was created successfully!')) I talked about automation earlier. It comes with a templating engine, Handlebars. It enables creating multiple images using the same template.

How to display data from an image in an HTML page?

Thanks You can display your data by creating a string of the image file in your component like so: And then binding this string to the src property of an img tag in the view like so: While the above solution should work it would be better to just create the url to the image on the client side and then reference the url in the html.

How to set up handlebars view engine in Node JS?

You may refer to this article for setting up handlebars View Engine in Node.js. app.listen (PORT, () => console.log (`Server started running on PORT $ {PORT}`)); Change the default view engine to handlebars: To serve static files like images from a directory named “images”


1 Answers

First, you have to set the public (folder) as static under server.js file

server.js

// configure our application
const Express = require('express');
const app = new Express();
.......
.......
app.use(Express.static(__dirname+'/public'));
.......

then your img source will be

<img class="logo" src="/images/Heading.png" alt="My_Logo">

and here the folders structure would be -

 project(root folder) \ public \ images\ Heading.png
like image 104
Soura Ghosh Avatar answered Sep 20 '22 18:09

Soura Ghosh