So I'm trying to see how to import HTML into my Node.js file. The HTML file is named index.html and the JS is named script.js. I was trying to append "Hello World!" but it only says "Node.js Stuff"
Here is what I've tried:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.write("Hello World!");
res.end();
});
}).listen(8080);
and this is the HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 class = "heading">Node.js Stuff</h1>
</body>
</html>
Install express:
npm i express
Node.js:
const express = require("express")
const app = express()
app.use(express.static(__dirname)
app.get("/",(req,res)=>{
res.sendFile("index.html")
}
app.listen(3000,()=>{console.log("Started")})