Nodejs Basic Notes

Source: Internet
Author: User

Bar seven days learn Nodejs (http://nqdeng.github.io/7-days-nodejs/) and re-read it again, with spring and spring Angry said blog backstage himself write read = = And then a wake up wrong ah This is what ghost, said all said Should try to do it. Come on, just. Fighting for tomorrow!

Learn a new thing, must first answer two questions, is this thing is what, what use, can do.

So the first question, what is Nodejs ?

JS is a scripting language, and the scripting language requires a parser to run. For JS written in the HTML page, the browser acts as the parser's role. For a js,nodejs that needs to run independently is a parser. Each parser is a running environment that not only allows JS to define various data structures, perform various calculations, but also allows JS to do something with the built-in objects and methods provided by the running environment. For example, the purpose of JS running in the browser is to manipulate the DOM, and the browser provides the document built-in objects. While the use of JS running in Nodejs is to manipulate disk files or build an HTTP server, Nodejs provides, and so on, the fs http built-in objects.

The second question, whatis the use of nodejs, what is capable .

Nodejs's author says that his goal of creating nodejs is to achieve a high-performance Web server, and he first values the superiority of the event mechanism and asynchronous IO model, not JS. But he needs to choose a programming language to implement his idea that the programming language does not have IO capabilities and needs to be able to support the event mechanism well. JS does not have an IO feature and is inherently used to handle DOM events in the browser, making it a natural choice.

as he wishes, Nodejs is active on the service side, and a large number of Nodejs-based Web services appear. On the other hand, Nodejs let the front end as the artifact, and finally let their ability to cover the range out of the browser window, a large number of front-end tools such as springing up. Therefore, for the front end, although not everyone has to take Nodejs to write a server program, but simple to use the command interactive mode debugging JS code snippets, complex to write tools to improve productivity.

Not only directly run JS file is a very cool thing, and with JS can write backstage can take server ah hello, really is very very cool thing ah. Even for the sake of pretending to be forced to write a try Ah, there are wood ~

OK, solve the two basic problems, then go back to the basics of things AH.

Module

Writing a slightly larger program typically blocks the code. In Nodejs, the code is generally reasonably split into different JS files, each file is a module, and the file path is the module name. When writing each module, there are, require exports and module three predefined variables to use.

  require

    requirefunction is used to load and use other modules in the current module, passing in a module name and returning a module export object. The module name can use a relative path ( ./ starting with), or an absolute path (beginning with a letter / or C: the like). In addition, the extension in the module name .js can be omitted. As follows:

    var foo1 = require(‘./foo‘);

Exports   

    exportsObject is an exported object of the current module that is used to export the module's public methods and properties. When other modules require use the current module through a function, they are the objects of the current module exports . In the following example, a public method is exported.

    exports.hello = function () {         console.log(‘Hello World!‘);    };
  
Module

modulesome information about the current module can be accessed through an object, but the most useful is to replace the exported object of the current module. For example, the module export object is a normal object by default and can be used in the following way if you want to change to a function.

    module.exports = function () {        console.log(‘Hello World!‘);    };

In the above code, the module default export object is replaced with a function.

The module passed to Nodejs by command-line arguments to start the program is called the main module . The main module is responsible for scheduling other modules that compose the entire program to complete the work. For example, when you start a program with the following command, it main.js is the main module.

  $ node main.js

  这些就是node很基础的一些内容,就吧大神的博客里面截取了一小部分放过来了一丢丢。前面讲nodejs能干什么的时候就有提过,nodejs可以操作磁盘文件,那么有很多文件操作的API,这个大神已经讲得很好了,等我真的把后台写好了,有什么想记录的东西了,再来写好了~还有网络操作,对应的内置对象和api,还有进程操作,异步编程,这些东西暂时只是跟着看还没动手,等写了之后懂了再过来写吧~~








Nodejs Basic Notes

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.