1

I am running a nodejs server file, code below. Currently the code you see does not have the HTML or CSS referenced because I don't know how to do it.

const http = require('http');

// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
    // Set a response type of plain text for the response
    res.writeHead(200, {'Content-Type': 'text/plain'});

    // Send back a response and end the connection
    res.end('Hello World!\n');
});

// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');

In a folder that looks like this

I have already searched online for hours, looked at previous stackoverflow posts, and I still haven't found the answer. I got the HTML file to load at one point, but I was not able to get the CSS file to load.

Simon
  • 19
  • 7
  • https://expressjs.com/en/starter/static-files.html – Dai Dec 02 '21 at 03:49
  • Tip: Search for the term "static files" for when you want to serve files from the filesystem directly - that way you can expose a directory and its contents without needing to add individual urls for each file you want to serve from that directory (like `*.html` and `*.css` files). – Dai Dec 02 '21 at 03:50
  • Does this answer your question? [Node.js quick file server (static files over HTTP)](https://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http) – Dai Dec 02 '21 at 03:50

1 Answers1

0

Html and Css files are static files so you need to search about serving static files with Node.js.

check this tutorial : How to serve static files

or this one: Node.js server without a framework

mohsen dorparasti
  • 8,107
  • 7
  • 41
  • 61
  • If the HTML file has events in it and scripts in it, like button clicks, will these events still occur even though the files are considered static and not dynamic? – Simon Dec 02 '21 at 03:54
  • Indeed. Once your html file is loaded in the client browser, everything works normally. – mohsen dorparasti Dec 02 '21 at 03:56
  • I am very grateful for your help. Peace and blessings on you. – Simon Dec 02 '21 at 03:57