Learning Nodejs is also out of curiosity about this new product, and there are two important projects that require his support. Therefore, you should study this new language well. In nodejs, modules can be divided into core modules and file modules. The core module is compiled into binary code. When referencing the code, you only need the require symbol to use nodejs for the project. It feels that nodejs is a front-end installation force, which is the only way to go to the full stack engineer, next, we will embark on 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 built-in module of nodejs, and start is a defined module.
2. index. js. This is the execution file. Note the require path.
var server=require("./module/server");server.start();
Run node index in the project directory with node. then, enter http: // localhost: 8888 in the browser to see the exciting hello, world, and Request recieved in the node terminal. The first program runs successfully.
The program module above is a folder containing the server. js file. Index. js is the same as the module folder.
Note:
The current directory of the relative path:./xxx. js or./xxx.
Parent directory of the relative path: ../xxx. js or ../xxx.
Absolute path: F:/xxx. js or/xxx. js or/xxx.
The above is all the content of this article. I hope you will like it.