In section 5.10 of our e-book course, we'll cover a fundamental topic for any NodeJS developer: creating a basic server and storing data in databases. We'll start with an introduction to the basic server, then dive into how you can store and retrieve data using databases.

Creating a Basic Server with NodeJS

NodeJS is a server-side development platform that allows you to use JavaScript, a programming language that is typically used for client-side scripting, to build web servers and networking applications. The first thing you need to do to create a basic server with NodeJS is install NodeJS and npm (Node Package Manager) on your computer.

Once you have NodeJS and npm installed, you can start creating your server. First, you need to create a new JavaScript file. You can name this file anything you like, but for this example, let's call it 'server.js'. In the 'server.js' file, you need to import the NodeJS 'http' module, which allows you to create a web server. Here's how you can do that:


var http = require('http');

After importing the 'http' module, you can use the 'createServer' function to create a new server. The 'createServer' function takes a callback function that is called whenever someone tries to access the server. This callback function takes two arguments: a request object, which contains information about the client's request, and a response object, which you can use to send a response to the client.


var server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

Finally, you need to call the 'listen' function on the server to start listening for connections. The 'listen' function takes a port number as an argument.


server.listen(3000, function() {
  console.log('Server running at http://localhost:3000/');
});

Data Storage in Databases

Now that you have a basic server up and running, you can start thinking about how to store and retrieve data. One of the most common ways to do this is using a database. NodeJS supports a variety of databases including MySQL, PostgreSQL, MongoDB and many others.

For this example, we are going to use MongoDB, a popular NoSQL database. To use MongoDB with NodeJS, you need to install the MongoDB driver for NodeJS using npm:


npm install mongodb

Once you have the MongoDB driver installed, you can start using MongoDB on your server. First, you need to import the 'mongodb' module in your 'server.js' file:


var mongodb = require('mongodb');

You can then use the 'MongoClient.connect' function to connect to your MongoDB database. The 'MongoClient.connect' function takes a connection string as an argument, which specifies the location of your MongoDB database.


mongodb.MongoClient.connect('mongodb://localhost:27017/mydatabase', function(err, db) {
  if (err) {
    console.log(err);
    return;
  }

  // You can now use the 'db' object to interact with your database.
});

Once you are connected to your database, you can use the 'db' object to interact with your database. For example, you can use the 'db.collection' function to get a collection from your database, and the 'insert' function to insert documents into your collection:


var collection = db.collection('mycollection');
collection.insert({name: 'John Doe', age: 30}, function(err, result) {
  if (err) {
    console.log(err);
    return;
  }

  // The 'result' object contains information about the insert operation.
});

We hope this section has given you a good understanding of how to create a basic server with NodeJS and how to store and retrieve data using MongoDB. In the next section, we'll dig deeper into data manipulation with NodeJS and MongoDB, including how to update, delete, and query data.

Now answer the exercise about the content:

What is the process for creating a basic server with NodeJS and storing data using MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Creating a basic server with NodeJS: Handling errors and exceptions

Next page of the Free Ebook:

42Creating a basic server with NodeJS: Handling errors and exceptions

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text