Chapter 2. Taking a Deep Dive into Node.js and MongoDB

A built-in asynchronous I/O, single-threaded or even multithreaded, real-time applications, sockets, and HTTP connections make Node.js the most powerful tool to create web servers that run JavaScript on the server side.

MongoDB is agile, scalable, document-oriented schema less, and high performance, which makes it one of the most popular NoSQL databases. Node.js and MongoDB unleashed all the power for high-performance web applications with Express, a lightweight web framework that supports template engines, routing, and flash notices. It's very similar to Sinatra from Ruby. Moving forward, we will see some of the key concepts about all of these tools.

The following will be covered in the chapter:

  • Node server, NPM, and middlewares
  • Event-driven development and the event loop
  • Working with require() and modules
  • Express, a web framework on a server
  • MongoDB and the terminal
  • The MongoDB connection with Mongoose
  • Mongoose schemas and models

Node server, NPM, and middleware

Node is a web server built with JavaScript and the basic Node.js proposal is of high performance and scalable web applications, but what does it really mean?

In other server-side programming languages, such as Java, PHP, and .NET, each connection starts a new thread on the server potentially, and in general is accompanied by 2 MB of memory. On a server that has 8 GB of RAM, this defines the maximum number of concurrent connections as about 4,000 users. Now, imagine that your application is growing and every day gets more and more hits; you probably will need more servers or even to invest in more hardware. This is the bottleneck of the whole architecture of web applications, the maximum number of concurrent I/O connections a server can handle and keep stable. Node.js solves this by changing how a connection is made to the server. Instead of starting a new thread for each connection, it creates a process that does not require a memory block to accompany it. It does not block outgoing calls directly to I/O.

Node servers can support tens of thousands of simultaneous connections. It does not allocate one thread per connection model, but uses a model process per connection, creating only the memory that is required for each connection. It achieves its goals by providing highly scalable servers.

Actually, Node changes the panorama of the server and the bottleneck of the whole system from the maximum number of connections to the maximum traffic capacity of the system. It also features a powerful package manager called NPM (Node Package Manager); though it is possible to install and update packages, you can still create your own packages and publish them online for the whole community. It is also worth noting that many third-party tools, such as Grunt, Bower, and Yeoman among others, use Node to install packages.

NPM is a command-line utility that interacts with an online open source repository of projects for Node.js; it is possible to install modules and manage dependencies and versions.

The NPM repository already has more than 76,000 packages, all of which are open source libraries, and many are added every day. These applications can be found through the search portal of NPM. You can search and discover a lot of packages at the official repository at https://www.npmjs.org/. You can found out more about Node.js at http://nodejs.org/.

Another important use of NPM is the ability to install its own modules and dependencies. When you have a Node project with a file named package.json, you can run the npm install command in the root folder of your project and NPM will install all dependencies listed in package.json.

In addition, the NPM init command starts the configuration of your project through a step-by-step configuration to create a package.json file from scratch.

The most used commands are:

  • npm install module_name: This installs a module in the project
  • npm install module_name-save: This installs the module and adds it to the list of dependencies on package.json
  • npm list: This lists all modules in the project
  • npm list-g: This lists all global modules
  • npm remove module_name: This uninstalls a module
  • module_name npm update: This updates the module version
  • npm -v: This displays the current version of NPM
  • npm adduser username: This creates a user on the NPM repository
  • npm whoami: This displays details of your public profile on the NPM repository
  • npm publish: This publishes your module on the NPM repository

Another key part of Node is called middleware. We often define middleware as bridges between snippet codes where all the flow passes through them. On a Node environment, this is very common. We have a lot of Middleware such as connect, body-parser (very useful to work with JSON), cookie parser, and many more. These are part of the Express (v4) web framework as separate modules, and we will see more about them later in the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236