2

I just started Node.js and have been following a tutorial on Node.js in w3school.

var http = require('http');
var url = require('url');
var fs = require('fs');  
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
  res.writeHead(404, {'Content-Type': 'text/html'});
  return res.end("404 Not Found");
} 
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);

I get the result fine. However, I didn't understand the dot(period) part. In this tutorial, when you type the http://localhost:8080/summer.html on your browser, you are supposed to get summer.html. (summer.html is made previously) In the code, when I parse the url, and pathname it, I will get /summer.html , but why do I need a period in front? NodeJS will read ./summer.html?

Because of the answer, now I know that Node.js uses the same file location format as HTML. This question might help those who didn't know this.

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
  • Possible duplicate of [What does "./" (dot slash) refer to in terms of an HTML file path location?](https://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location) – Leo Apr 25 '19 at 08:58

2 Answers2

6

. means "The current directory".

So ./summer.html means "summer.html in the current directory" while /summer.html means "summer.html in the root directory of the file system" (which probably doesn't exist).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

A ./ in front of the URL is equivalent to the current path.

one more thing - . is the current directory and .. is the previous directory.

/ at the beginning of a URL means the root directory

nipek
  • 810
  • 1
  • 9
  • 22