-1

I created a file called nodes, then initialized the file with npm init and the main js file is called main.js. I also created index.html and index.css in the file, after that I want to use Node.js Render this index.html, so I wrote in main.js:

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 9000;
const mainHTML = './index.html';

const server = http.createServer((req, res) => {
  fs.stat(`./${mainHTML}`, (err, stats) => {
    if(stats) {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/html');
      fs.createReadStream(mainHTML).pipe(res);
    }
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

I opened the server with the node desktop/nodes command, but node.js could not find the file.

Until I changed the relative path to an absolute path, Node.js will recognize it:

const mainHTML = 'desktop/nodes/index.html';

Why is this? If I want to use a relative path, how do I do it?

Shao Kahn
  • 67
  • 2
  • 11
  • I think one of the problems was the duplication of the `./` part. `const mainHTML = './index.html';` sets mainHTML to "./index.html", and `./${mainHTML}` in the fs.stat() method evaluates to `././index.html` – Samathingamajig Oct 27 '20 at 00:25

2 Answers2

2

When you access a file in node.js with a relative path, the file is accessed relative to the value of the current working directory for the process. Note, in the modular world of node.js, the current working directory may or may not be the same as the directory where your module was located. And, your code can change the current working directory to be whatever you want it to be.

It is common in modular node.js code to have a programming desire to access things relative to the directory where the current module's code was loaded from. This gives you the ability to use relative paths so the app/module can work anywhere, but it gives you certainty that you'll get the files you want. To do this, one typically uses the module-specific variable __dirname. This is the directory that the current module was loaded from. If it's the main script that node.js was started with then, it's the directory of that script.

So, to get a file from the same directory as the script you are current in, you would do this:

const mainHTML = 'index.html';
fs.createReadStream(path.join(__dirname, mainHTML)).pipe(res);

To access a file in a subdirectory public below where the script is, you could do this:

const mainHTML = 'public/index.html';
fs.createReadStream(path.join(__dirname, mainHTML)).pipe(res);

To access a file in a different subdirectory at the same level (common parent directory) as where the script is, you could do this:

const mainHTML = '../public/index.html';
fs.createReadStream(path.join(__dirname, mainHTML)).pipe(res);

All of these use paths that are relative to where the script itself is located and do not depend upon how the module/script was loaded or what the current working directory of the app is.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You are creating http server, which creates it's path as base, so it understands only paths taking that base path as relative path. If you want to use relative path, then you need to resolve that path.

You can use 'path' library.

const path = require('path')
// To resolve parent path
path.resolve('..', __dirname__)
Vikas Keskar
  • 1,158
  • 9
  • 17