how to read html content in node js code example
Example 1: read html file node js
const http = require("http");
//use fs module at first to read file
const fs = require("fs");
const hostname = "127.0.0.1";
const port = 3000;
// simple code to read file using fs module
const files = fs.readFileSync("new.html");
const server = http.createServer((req, res) => {
res.statusCode = 200;
// give correct input for html
res.setHeader("Content-Type", "text/html");
res.end(files);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
console.log("Done")
});
//simple code to make server and read file
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>