On the problem of require path in Nodejs

Source: Internet
Author: User
Tags parent directory relative require

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.