Active14 days ago
Update: I wrote this answer over a year ago, and since that time there has an explosion in the number of great resources available for people learning Node.js. Though I still believe diving into the source is worthwhile, I think that there are now better ways to get started. The API reference documentation provides detailed information about a function or object in Node.js. This documentation indicates what arguments a method accepts, the return value of that method, and what errors may be related to that method. It also indicates which methods are available for.
- This tutorial is for Node.js and Express 4 Beginners. Express is a node.js web development framework which comes with various features such as rendering, routing, REST controls. In this post I am not going to use JADE which is a markup scheme used in most of Express project, instead, I am using basic HTML page as mark up language.
- In learn nodejs complete guide tutorials for free you’re going to learn everything about node.js MVC, GraphQL, REST APIs, SQL, MongoDB or more, also you can download all video tutorials for free but before download the course you must be read following topics.
Are there any good resources to get started with Node.JS? Any good tutorials, blogs or books?
Of course, I have visited its official website http://nodejs.org/, but I didn't think the documentation they have is a good starting point.
closed as off-topic by Ry-♦Feb 19 '18 at 12:12
This question appears to be off-topic. The users who voted to close gave this specific reason:
- 'Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.' – Ry-
locked by Shog9♦Jul 19 '13 at 22:30
This question’s answers are a collaborative effort. If you see something that can be improved, just edit the answer to improve it! No additional answers can be added here.
Read more about locked posts here.
3 Answers
Tutorials
- Hello World Web Server (paid)
Developer Sites
Videos
- Node Tuts (Node.js video tutorials)
Screencasts
Books
Courses
Blogs
Podcasts
JavaScript resources
- Crockford's videos (must see!)
- JavaScript Patterns book
- JavaScript: The Good Parts book
- Eloquent javascript book
Node.js Modules
- Wiki List on GitHub/Joyent/Node.js (start here last!)
Other
- Production sites with published source:
First, learn the core concepts of Node.js:
- You'll want to understand the asynchronous coding style that Node.js encourages.
- Async != concurrent. Understand Node.js's event loop!
- Node.js uses CommonJS-style require() for code loading; it's probably a bit different from what you're used to.
- Familiarize yourself with Node.js's standard library.
Then, you're going to want to see what the community has to offer:
The gold standard for Node.js package management is NPM.
- It is a command line tool for managing your project's dependencies.
- Make sure you understand how Node.js and NPM interact with your project via the node_modules folder and package.json.
Finally, you're going to want to know what some of the more popular packages are for various tasks:
Useful Tools for Every Project:
- Underscore contains just about every core utility method you want.
- Lo-Dash is a clone of Underscore that aims to be faster, more customizable, and has quite a few functions that underscore doesn't have. Certain versions of it can be used as drop-in replacements of underscore.
- TypeScript makes JavaScript considerably more bearable, while also keeping you out of trouble!
- JSHint is a code-checking tool that'll save you loads of time finding stupid errors. Find a plugin for your text editor that will automatically run it on your code.
Unit Testing:
- Mocha is a popular test framework.
- Vows is a fantastic take on asynchronous testing, albeit somewhat stale.
- Expresso is a more traditional unit testing framework.
- node-unit is another relatively traditional unit testing framework.
- AVA is a new test runner with Babel built-in and runs tests concurrently.
Web Frameworks:
- Express.js is by far the most popular framework.
- Koa is a new web framework designed by the team behind Express.js, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.
- sails.js the most popular MVC framework for Node.js, and is based on express. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps:> Not the answer you're looking for? Browse other questions tagged javascriptnode.js or ask your own question.
Mostly all modern-day web applications have some sort of data storage system at the backend. For example, if you take the case of a web shopping application, data such as the price of an item would be stored in the database.
The Node js framework can work with databases with both relational (such as Oracle and MS SQL Server) and non-relational databases (such as MongoDB). In this tutorial, we will see how we can use databases from within Node js applications.
In this tutorial, you will learn-
Node.js and NoSQL Databases
Over the years, NoSQL database such as MongoDB and MySQL have become quite popular as databases for storing data. The ability of these databases to store any type of content and particularly in any type of format is what makes these databases so famous.
Node.js has the ability to work with both MySQL and MongoDB as databases. In order to use either of these databases, you need to download and use the required modules using the Node package manager.
For MySQL, the required module is called 'mysql' and for using MongoDB the required module to be installed is 'Mongoose.'
With these modules, you can perform the following operations in Node.js
- Manage the connection pooling – Here is where you can specify the number of MySQL database connections that should be maintained and saved by Node.js.
- Create and close a connection to a database. In either case, you can provide a callback function which can be called whenever the 'create' and 'close' connection methods are executed.
- Queries can be executed to get data from respective databases to retrieve data.
- Data manipulation, such as inserting data, deleting, and updating data can also be achieved with these modules.
For the remaining topics, we will look at how we can work with MongoDB databases within Node.js.
Using MongoDB and Node.js
As discussed in the earlier topic, MongoDB is one of the most popular databases used along with Node.js.
During this chapter, we will see
How we can establish connections with a MongoDB database
How we can perform the normal operations of reading data from a database as well as inserting, deleting, and updating records in a MongoDB database.
For the purpose of this chapter, let's assume that we have the below MongoDB data in place.
Database name: EmployeeDB Ipod touch drivers windows 2000.
Collection name: Employee
- Installing the NPM Modules
You need a driver to access Mongo from within a Node application. There are a number of Mongo drivers available, but MongoDB is among the most popular. To install the MongoDB module, run the below command
npm install mongodb
- Creating and closing a connection to a MongoDB database. The below code snippet shows how to create and close a connection to a MongoDB database.
Code Explanation:
- The first step is to include the mongoose module, which is done through the require function. Once this module is in place, we can use the necessary functions available in this module to create connections to the database.
- Next, we specify our connection string to the database. In the connect string, there are 3 key values which are passed.
- The first is 'mongodb' which specifies that we are connecting to a mongoDB database.
- The next is 'localhost' which means we are connecting to a database on the local machine.
- The next is 'EmployeeDB' which is the name of the database defined in our MongoDB database.
- The next step is to actually connect to our database. The connect function takes in our URL and has the facility to specify a callback function. It will be called when the connection is opened to the database. This gives us the opportunity to know if the database connection was successful or not.
- In the function, we are writing the string 'Connection established' to the console to indicate that a successful connection was created.
- Finally, we are closing the connection using the db.close statement.
If the above code is executed properly, the string 'Connected' will be written to the console as shown below.
- Querying for data in a MongoDB database – Using the MongoDB driver we can also fetch data from the MongoDB database. The below section will show how we can use the driver to fetch all of the documents from our Employee collection in our EmployeeDB database. This is the collection in our MongoDB database, which contains all the employee-related documents. Each document has an object id, Employee name, and employee id to define the values of the document.
Code Explanation:
- In the first step, we are creating a cursor (A cursor is a pointer which is used to point to the various records fetched from a database. The cursor is then used to iterate through the different records in the database. Here we are defining a variable name called cursor which will be used to store the pointer to the records fetched from the database. ) which points to the records which are fetched from the MongoDb collection. We also have the facility of specifying the collection 'Employee' from which to fetch the records. The find() function is used to specify that we want to retrieve all of the documents from the MongoDB collection.
- We are now iterating through our cursor and for each document in the cursor we are going to execute a function.
- Our function is simply going to print the contents of each document to the console.
Note: - It is also possible to fetch a particular record from a database. This can be done by specifying the search condition in the find() function. For example, suppose if you just wanted to fetch the record which has the employee name as Guru99, then this statement can be written as follows
If the above code is executed successfully, the following output will be displayed in your console.
Output:
From the output,
- You will be able to clearly see that all the documents from the collection are retrieved. This is possible by using the find() method of the mongoDB connection (db) and iterating through all of the documents using the cursor.
- Inserting documents in a collection – Documents can be inserted into a collection using the insertOne method provided by the MongoDB library. The below code snippet shows how we can insert a document into a mongoDB collection.
Code Explanation:
- Here we are using the insertOne method from the MongoDB library to insert a document into the Employee collection.
- We are specifying the document details of what needs to be inserted into the Employee collection.
If you now check the contents of your MongoDB database, you will find the record with Employeeid of 4 and EmployeeName of 'NewEmployee' inserted into the Employee collection.
Note: The console will not show any output because the record is being inserted in the database and no output can be shown here.
To check that the data has been properly inserted in the database, you need to execute the following commands in MongoDB
- Use EmployeeDB
- db.Employee.find({Employeeid :4 })
The first statement ensures that you are connected to the EmployeeDb database. The second statement searches for the record which has the employee id of 4.
- Updating documents in a collection - Documents can be updated in a collection using the updateOne method provided by the MongoDB library. The below code snippet shows how to update a document in a mongoDB collection.
Code Explanation:
- Here we are using the 'updateOne' method from the MongoDB library, which is used to update a document in a mongoDB collection.
- We are specifying the search criteria of which document needs to be updated. In our case, we want to find the document which has the EmployeeName of 'NewEmployee.'
- We then want to set the value of the EmployeeName of the document from 'NewEmployee' to 'Mohan'.
If you now check the contents of your MongoDB database, you will find the record with Employeeid of 4 and EmployeeName of 'Mohan' updated in the Employee collection.
To check that the data has been properly updated in the database, you need to execute the following commands in MongoDB
- Use EmployeeDB
- db.Employee.find({Employeeid :4 })
The first statement ensures that you are connected to the EmployeeDb database. The second statement searches for the record which has the employee id of 4.
- Deleting documents in a collection - Documents can be deleted in a collection using the 'deleteOne' method provided by the MongoDB library. The below code snippet shows how to delete a document in a mongoDB collection.
Code Explanation:
- Here we are using the 'deleteOne' method from the MongoDB library, which is used to delete a document in a mongoDB collection.
- We are specifying the search criteria of which document needs to be deleted. In our case, we want to find the document which has the EmployeeName of 'Mohan' and delete this document.
If you now check the contents of your MongoDB database, you will find the record with Employeeid of 4 and EmployeeName of 'Mohan' deleted from the Employee collection.
To check that the data has been properly updated in the database, you need to execute the following commands in MongoDB
- Use EmployeeDB
- db.Employee.find()
The first statement ensures that you are connected to the EmployeeDb database. The second statement searches and display all of the records in the employee collection. Here you can see if the record has been deleted or not.
How to build a node express app with MongoDB to store and serve content
Building an application with a combination of both using express and MongoDB is quite common nowadays.
When working with JavaScript web based applications, one will normally here of the term MEAN stack.
- The term MEAN stack refers to a collection of JavaScript based technologies used to develop web applications.
- MEAN is an acronym for MongoDB, ExpressJS, AngularJS, and Node.js.
Hence, it's always good to understand how Node.js and MongoDB work together to deliver applications which interact with backend databases.
Let's look at a simple example of how we can use 'express' and 'MongoDB' together. Our example will make use of the same Employee collection in the MongoDB EmployeeDB database.
We will now incorporate Express to display the data on our web page when it is requested by the user. When our application runs on Node.js, one might need to browse to the URL http://localhost:3000/Employeeid.
When the page is launched, all the employee id in the Employee collection will be displayed. So let's see the code snippet in sections which will allow us to achieve this.
Step 1) Define all the libraries which need to be used in our application, which in our case is both the MongoDB and express library.
Code Explanation:
- We are defining our 'express' library, which will be used in our application.
- We are defining our 'express' library, which will be used in our application for connecting to our MongoDB database.
- Here we are defining the URL of our database to connect to.
- Finally, we are defining a string which will be used to store our collection of employee id which need to be displayed in the browser later on.
Step 2) In this step, we are now going to get all of the records in our 'Employee' collection and work with them accordingly.
Code Explanation:
- We are creating a route to our application called 'Employeeid.' So whenever anybody browses to http://localhost:3000/Employeeid of our application, the code snippet defined for this route will be executed.
- Here we are getting all of the records in our 'Employee' collection through the db.collection('Employee').find() command. We are then assigning this collection to a variable called cursor. Using this cursor variable, we will be able to browse through all of the records of the collection.
- We are now using the cursor.each() function to navigate through all of the records of our collection. For each record, we are going to define a code snippet on what to do when each record is accessed.
- Finally, we see that if the record returned is not null, then we are taking the employee via the command 'item.Employeeid'. The rest of the code is just to construct a proper HTML code which will allow our results to be displayed properly in the browser.
Step 3) In this step, we are going to send our output to the web page and make our application listen on a particular port.
Code Explanation:
- Here we are sending the entire content which was constructed in the earlier step to our web page. The 'res' parameter allows us to send content to our web page as a response.
- We are making our entire Node.js application listen on port 3000.
Node.js Tutorial Free Download
Output:
From the output,
- It clearly shows that all of the employeeid's in the Employee collection were retrieved. This is because we use the MongoDB driver to connect to the database and retrieve all the Employee records and subsequently used 'express' to display the records.
Here is the code for your reference
Note: cursor.each maybe deprecated based on version of your MongoDB driver. You can append //noinspection JSDeprecatedSymbols before cursor.each to circumvent the issue. Alternatively, you can use forEach. Below is the sample code using forEach
Summary
- Node.js is used in conjunction with NoSQL databases to build a lot of modern days web applications. Some of the common databases used are MySQL and MongoDB.
- One of the common modules used for working with MongoDB databases is a module called 'MongoDB.' This module is installed via the Node package manager.
- With the MongoDB module, it's possible to query for records in a collection and perform the normal update, delete and insert operations.
- Finally, one of the modern practices is to use the express framework along with MongoDB to deliver modern-day applications. The Express framework can make use of the data returned by the MongoDB driver and display the data to the user in the web page accordingly.