- Web Development with MongoDB and NodeJS(Second Edition)
- Mithun Satheesh Bruno Joseph D'mello Jason Krol
- 540字
- 2021-07-09 21:13:01
The basics of Node.js
With the basics of JavaScript out of the way, let's focus on some of the basics of Node.
Event-driven
At its core, one of the most powerful features of Node is that it is event-driven. This means that almost all the code you write in Node is going to be written in a way that is either responding to an event or is itself firing an event (which in turn will fire other code listening for that event).
Let's take a look at the code that we'll write in a later chapter that handles connecting to a MongoDB server using Mongoose, a popular Node.js MongoDB Object Document Mapper (ODM) module:
mongoose.connect('); mongoose.connection.on('open', function() { console.log("Connected to Mongoose..."); });
First, we tell our mongoose
object to connect to the server provided as a string parameter to the function. Connecting will take an undetermined amount of time though, and we have no way of knowing how long. So, what we do is bind a listener to the open
event on the mongoose.connection
object. With the use of the on keyword, we are indicating that when the mongoose.connection
object triggers an open
event, it executes the anonymous function that was passed in as the parameter.
Asynchronous execution
Earlier, we reviewed the idea of asynchronous JavaScript code in the browser using setTimeout
—the principles apply more strongly in the world of Node. As you may be making a number of network-dependent connections to different REST API services, database servers, and anything else, it is important that your code can execute smoothly and has proper callback usage in place whenever each service responds.
The module system
In an effort to make the code as modular and reusable as possible, Node uses a module system that allows you to better organize your code. The basic premise is that you write a code fulfilling a single concern, and export this code as a module that serves that single purpose. Then, whenever you need to use that code elsewhere in your code base, you will require that module:
// ** file: dowork.js module.exports = { doWork: function(param1, param2) { return param1 + param2; } } // ** file: testing.js var worker = require('./dowork'); // note: no .js in the file var something = 1; var somethingElse = 2; var newVal = worker.doWork(something, somethingElse); console.log(newVal); // => 3
Using this system, it is simple to reuse the functionality in a module (in this case, the dowork
module) in a number of other files. Furthermore, the individual files of a module act as a private namespace. Any variables declared and used within the module file are private to that module and not exposed to any code that uses the module via require()
.
This system extends infinitely as well. Within your modules, you can require other modules and so on and so forth.
The Node.js core
The Node.js core literally has hundreds of modules available for you to use while writing your applications. These include the following:
- Events
- Filesystems
- HTTP
- Net
- Streams
- Timers
Tip
Definitely make sure to check out the online docs on Node (at http://nodejs.org/api) to see the full list of modules available in Node's core and see plenty of sample code and explanations.
- LabVIEW 2018 虛擬儀器程序設計
- HTML5移動Web開發技術
- 工程軟件開發技術基礎
- Vue.js 2 and Bootstrap 4 Web Development
- TypeScript圖形渲染實戰:基于WebGL的3D架構與實現
- 手把手教你學C語言
- Python機器學習算法: 原理、實現與案例
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- Learning AWS
- Laravel Application Development Blueprints
- HTML5+CSS3+jQuery Mobile APP與移動網站設計從入門到精通
- Learning VMware vSphere
- Deep Learning for Natural Language Processing
- 面向對象程序設計及C++(第3版)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐