About the require path in NodeJS _ node. js-js tutorial

Source: Internet
Author: User
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.

Related Article

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.