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.
var http = require ("http");
function Start () {
function onrequest (request, Response) {
Console.log ("request recieved")
Response.writehead ({
"Content-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.
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.