官术网_书友最值得收藏!

Module basics

In this section, you will finally learn some Node.js code, and we'll kick things off by talking about modules inside Node. Modules are units of functionality, so imagine I create a few functions that do something similar, such as a few functions that help with math problems, for example, add, subtract, and divide. I could bundle those up as a module, call it Andrew-math, and other people could take advantage of it.

Now, we'll not be looking at how to make our own module; in fact, we will be looking at how we can use modules, and that will be done using a function in Node, called require(). The require() function will let us do three things:

  • First, it'll let us load in modules that come bundled with Node.js. These include the HTTP module, which lets us make a web server, and the fs module, which lets us access the filesystem for our machine.
We will also be using require() in later sections to load in third-party libraries, such as Express and Sequelize, which will let us write less code.
  • We'll be able to use prewritten libraries to handle complex problems, and all we need to do is implement require() by calling a few methods.
  • We will use require() to require our very own files. It will let us break up our application into multiple, smaller files, which is essential for building real-world apps.

If you have all of your code in one file, it will be really hard to test, maintain, and update. Now, require() isn't that bad. In this section, we'll explore the first use case for require().

主站蜘蛛池模板: 马尔康县| 梁平县| 蒲江县| 麻栗坡县| 凯里市| 蕲春县| 奈曼旗| 田东县| 涞源县| 长顺县| 侯马市| 张掖市| 宁武县| 德保县| 昆明市| 南投市| 和林格尔县| 华坪县| 博湖县| 乌鲁木齐县| 秭归县| 富顺县| 通许县| 新蔡县| 石阡县| 思茅市| 云浮市| 鱼台县| 乌鲁木齐市| 鲜城| 库尔勒市| 乐东| 惠州市| 洛阳市| 肥东县| 哈尔滨市| 荥阳市| 深圳市| 淮北市| 北川| 缙云县|