Projects need to use Nodejs, feel Nodejs is the front-end installed force artifact, is to the full stack of engineers, the way to the next start to embark on the journey of learning Nodejs. The following 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 module, HTTP is the Nodejs module, start is a module of its own definition.
2, Index.js. This is the path of the execution file, note require.
var server=require ("./module/server"); Server.start ();
In the project directory, run node index.js with node and enter it in the browser: http://localhost:8888 can see the exciting hello,world, and you can see the request recieved in the node terminal. The first program runs successfully.
The above program module is a folder that contains the Server.js file. 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.
NodeJS Require path