Learning Nodejs is also out of curiosity about the new product, and there are two important projects that need his support, so learn to learn the new language well. In Nodejs, modules can be divided into core modules and file modules. The core module is compiled into binary code, which can be referenced only by require notation.
Projects need to use Nodejs, feeling Nodejs is the front end of the installation of the artifact, is the only way to the entire stack of engineers Wow, then began to embark on the journey of learning Nodejs. Here is the first Hello,world program.
1, server.js file, which is equivalent to server script.
?
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
var http = require ("http"); function start () {function onrequest (request, Response) {Console.log ("request recieved") response.writehead (= "Con Tent-type ":" Text/plain "}); Response.Write ("Hello,world"); Response.End (); } http.createserver (ONrequest). Listen (8888); } Exports.start=start; |
This is the simplest of a module, HTTP is the Nodejs of the module, start is its own definition of a module.
2, Index.js. This is the execution file, note the Require path.
?
1 2 |
var server=require ("./module/server"); Server.start (); |
Run node index.js with node in the project directory and enter it in the browser: http://localhost:8888 can see the exciting hello,world, and the request recieved can be seen in the node terminal. The first program ran successfully.
The program module above is a folder that contains server.js files. Index.js is similar to the module folder.
Note the Require path:
Current directory of relative paths:./xxx/xxx.js or./xxx/xxx.
Parent directory of Relative path:.. /xxx/xxx.js or ... /xxx/xxx.
Absolute path: F:/xxx/xxx.js or/xxx/xxx.js or/xxx/xxx.
The above mentioned is the entire content of this article, I hope you can enjoy.