This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging

🎉 preface

  • I remember my first contact with learningnodeWhen still follow rookie tutorial to learn one by one, time has been quite long, usually if not how to use the words will slowly forget some 😅.
  • When learning at that time, I feel a little confused, I do not know why to learn 🤯, some companies rarely need you willnodeNot even. I’m sure there are a lot of beginner players who think the same 🤧. With this in mind, I want to relearnnodeAnd stoodLearners’The point of view ofSharing with empathyTake a look at thisEnough refers to the northSeries, hope to help you 😆.
  • I believe you want to learnnodeAlso in order to be able to write their own data to show, to achieve the increase, delete, check and change, this series in order to take care of the novice students will talk about the basis of comparison, the purpose is to meet the front endnodetheenoughThe target.
  • This article mainly sharesnodetheInstallation and module system, the full text2000 +Words, reading may take ten minutes. specificRefers to the wizardAt the bottom of the article, will continue to update oh, welcome to like the collection ❤️❤️~

👻 about NodeJS

Node.js is a Javascript runtime environment based on Chrome’S V8 engine

  • In our normal development, most of theWebProjects, front-end are usedJSThe backend is written in the server language, such asJAVA PHP GOBut becauseNodeSo that front-end developers can also use itJSTo write server code, soNodeIt can be said that the birth of the front end is to let the front end of the two bloom.
  • In particular,NodeProvides the foundation for the development environment to run, as we often useVue ReactThis front-end framework becomes so powerful that it becomes an essential piece of front-end infrastructure.
  • NodeMore people use it. We use it on the front endJSMore and more people are contributing to open source,npmThe library becomes a very, very large repository of code innpmIn this package management system, we can find all the plugins and wheels we need, and we use it directly, which saves us a lot of precious time as developers.
  • For moreNodeJSThe introduction and function in this article has been introduced very clearly, everyone interested can go to have a lookportal.

👻 NodeJS installation

  • NodeJSWe can get to himThe official websiteDownload.

  • Choose your own version to download, since my computer has already installed, I will not demonstrate, just open the installation program and the next step can be seen, for more detailed installation guide and global configuration guide can see :Node.js Installation and Environment configuration for Windows
  • After the installation, we need to check whether the installation is successful and open the terminal inputnode -v.

  • You can see that we have the latest version installed16.6.1thenode.

Use 👻 NodeJS

  • We’re finishing the installationnodeCan be typed in the terminalnodeEnter interactive mode and enter a piece of code we must have for programmershello world.

  • Of course we can build oneJSFile to execute.
echo test>helloWorld.js
Copy the code
/* helloworld.js */ console.log(' helloWorld!! ')Copy the code

  • We just type innode+The file nameThis file can be executed.

👻 NodeJS module

  • NodeThe application consists of modules adopted byCommonJSModule specification. Each file is a module, with its own scope, variables, functions, etc., and is not visible to other modules. The file path is the module name, so we need to understand how different modules interact and use each other.
  • Modules can be loaded multiple times, but only run once on the first load, in the order in which they appear in the code.
  • In writing each module, there arerequire,exports,moduleThree predefined variables are available.

🍬 load (require)

  • requireIt means needed, which means we can get throughrequireTo introduce the modules that we need,
let x=require('./hello')
let y=require('./hello.js')
Copy the code
  • We can introduce a module like this,requireYou can then receive an address, either an absolute path or a relative path.
  • It’s worth noting that our suffix.jsThe extension can be omitted.

🍬 exports (exports)

  • exportsIt means to derive, but there is also an expression called expose, which we can use in generalexportsUsed to export module public functions and properties.
/* hinode.js */ export.addice =function(){console.log(' I'm adding ice ')}Copy the code
  • We are inhiNode.jsA function is exposed in the fileaddIceSo we can be somewhere elserequireThis module also uses the function.
/* helloWorld.js */
let x=require('./hiNode')
x.addIce()
Copy the code
  • We type in the terminalnode helloWorld.js.

  • Successfully introduced the module and used the function.

🍬 Module object (module)

  • inNodeIn which we passmoduleYou can access some information about the current module.
/* hinode.js */ export.addice =function(){console.log(' I'm adding ice ')} console.log(module)Copy the code

  • You can see what we printedmoduleIt has the function it exported, the name of the file, the path and so on. In fact when werequireModule, it reads from that filemodule.exportsThe variable.
  • thismodule.exportsVariables are usually in the form of objects as shown in the figure above, so we often use them most frequentlymoduleIs to rewritemodule.exportsThe variable, the derived variable, we can change it to a functional form.
/* hinode.js */ module.exports=function(){console.log(' ')}Copy the code
/* helloWorld.js */
let x=require('./hiNode')
x()
Copy the code
  • As above, we can use it directlyxbecauserequireThe module’s default export object is replaced by a function.

👋 at the end

  • On the wholeNodeJSApplications are made up of modules that we can use inJs fileexportexportsFunction and other variables in the otherJs fileFor the importrequireThis module.
  • This article as enough to use to refer to the north of the beginning of the end here, in order to take care of the new special basis, please forgive the big guys ha ~~
  • If you think this article is helpful to your words might as well 🍉 attention + thumbs up + favorites + comments + forwarding 🍉 support yo ~~😛

🌅 indicates the north wizard

“From scratch” front-end node can refer to north (1)⚡– installation and module

“Starting from zero” front-end node is sufficient to refer to north (2)⚡– file operations

“From zero” front-end node is sufficient to refer to north (3)⚡– network operations

“From scratch” front-end node is enough to refer to north (4)⚡– Express framework

“From scratch” front-end node can be used to refer to north (5)⚡– connect to the database

“From zero” front-end node can be used to refer to north (6)⚡- actual data large screen