use html file in node js code example

Example 1: show html file on nodejs code

let http = require('http');
let fs = require('fs');
 
let handleRequest = (request, response) => {
    response.writeHead(200, {
        'Content-Type': 'text/html'
    });
    fs.readFile('./index.html', null, function (error, data) {
        if (error) {
            response.writeHead(404);
            respone.write('Whoops! File not found!');
        } else {
            response.write(data);
        }
        response.end();
    });
};
 
http.createServer(handleRequest).listen(8000);

Example 2: how to use node.js in html file

<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>The Page Returned by Making Http Call to Node.js</title>
    <style type="text/css">
        table, td {
          border:double;
        }
    </style>
</head>
<body>
    <h1>Product Information Page</h1>
    <table>
        <tr>
            <td>Product Id:</td>
            <td>
                <input type="text" />
            </td>
        </tr>
        <tr>
            <td>Product Name:</td>
            <td>
                <input type="text" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button"  value="Save"/>
            </td>
        </tr>
    </table>
 
 
</body>
</html>

Tags:

Html Example