Home >Web Front-end >JS Tutorial >A brief discussion on the require path problem in NodeJS_node.js
The project needs to use nodejs. I feel that nodejs is a powerful front-end tool and the only way to become a full-stack engineer. Next, I will start the journey of learning nodejs. The following is the first hello, world program.
1. server.js file, which is equivalent to a server script.
var http = require("http"); function start() { function onRequest(request, response) { console.log("Request recieved") response.writeHead(200, { "Content-Type": "text/plain" }); response.write("hello,world"); response.end(); } http.createServer(onRequest).listen(8888); } exports.start=start;
This is the simplest module. http is the module that comes with nodejs, and start is a module defined by yourself.
2. index.js. This is the executable file, pay attention to the require path.
var server=require("./module/server"); server.start();
Use node to run node index.js in the project directory, and then enter: http://localhost:8888 in the browser to see the exciting hello and world, and you can also see it in the node terminal. Request received. The first program runs successfully.
The program module above is a folder containing the server.js file. index.js is at the same level as the module folder.
Note the require path:
Relative path to the current directory: ./xxx/xxx.js or ./xxx/xxx.
The upper-level directory on the relative path: ../xxx/xxx.js or ../xxx/xxx.
Absolute path: F:/xxx/xxx.js or /xxx/xxx.js or /xxx/xxx.
The above is the entire content of this article, I hope you all like it.