Complete REST API with MongoDB Atlas cloud, Node, and Express in 10 minutes

Let's bring the data to the cloud this time! 💾 ☁.
And what better way to do that than a FREE cloud database!
We'll be using the free cluster of Mongo DB Atlas.

What is Mongo DB Atlas?

Mongo DB Atlas is a fully-managed database-as-a-service available on AWS, Azure, and GCP

Before Atlas, I used mLab. mLab was then acquired by MongoDB last year. Pretty much similar to mLab, MongoDB Atlas is the easiest way to have a cloud MongoDB instance.

Why?

  • 💲 0 dollars: I am not an advertiser for MongoDB, but I love this service and this one won't break the bank ❌🔨🐖, since... well it's no-strings-attached FREE for up to 512MB. Perfect for small side-projects!

  • 🏃‍♂️ Quick, simple, and fast: no installation and config files. After signing up and creating a cluster (takes 5 minutes), you're all set.

  • ☁ It's in the cloud: Not running in your machine as a Mongo service. Not in a disk file like diskdb. You can manage it from anywhere through the Atlas web app. For your app, you only need a connection URL to access the db.

  • 😎 Very cool UI: Atlas dashboard is very intuitive, and first-time users will find it easy to use with the visual cues and steps.

Cloud setup

There might be some slight screenshot differences to the actual Atlas UI by the time you do this, but the described steps should get you to the same results.

1. First sign up here:

https://www.mongodb.com/cloud/atlas/register

2. Create a free tier Starter Cluster

The free Starter Cluster M0 comes with 512 MB Storage, shared RAM and vCPU.

This should be more than enough for our starter Node project.

  • Cluster region: you can pick any provider (AWS, Google, Azure) and the region closest to you that has a Free Tier. For me, it was AWS us-east-1.

  • You can leave the rest at FREE defaults.

  • Create the cluster.

The creation and provisioning might take a few minutes

  • Afterwards, the dashboard appears, with the side panel that contains all the options needed to manage clusters.

3. Connect to the Cluster

Click [Connect] in the Cluster we have just created.

The dialog that appears gives options to:

  1. Whitelist your IP address
  2. Create a MongoDB user

3.1 Whitelist IP Address

Whitelisting means only allowing access to selected (thus trusted) entities

  • [Add Your Current IP address], which should automatically get your current public IP address. Optionally add a description such as "My Home IP address"

    • ❗ Remember to do this step again when you switch WiFi, e.g. when moving from coffee shop to home WiFi.
    • 💡 If you are in a public wi-fi, you can use a service like https://whatismyipaddress.com/ to get your IPv4 public address, since ipconfig or ifconfig might give you only the internal network address.
    • 💣You can use 0.0.0.0/0 to enable all IP addresses anywhere, which simplifies this process, but makes it VERY INSECURE.

3.2 Create admin user.

Enter your preferred username and password. This will be the first admin ("root") user, then [Create MongoDB User].

  • Keep these credentials handy, since we'll use them later.

  • Close the window. We'll connect to the cluster later.

4. 📃 Add sample data

From the cluster view, click [Collections], then [Add my Own Data]

  • Same as the last lesson, we use:
    • database name: data
    • collection name: movies

🎥 The movies from this series is from IMDB. You could use the id from any movie page URL. E.g. In "The Lion King" page https://www.imdb.com/title/tt0110357id is tt0110357.

Insert Document

  • Insert a movie entry, as before - Leave the given _id provided by Atlas since this is internal

  • Now we see the inserted data in the dashboard. Add more as you wish

Perfect. Now we got data. Time to connect to the cluster

5. 🔌 Connect to cluster

Go back to the Clusters view, then click Connect in our cluster.

Choose [Connect your Application]

Leave the default Node.js version 3+

Copy the Connection String. We'll use this in the next step.

Now we're ready to dive into code!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
REST API 是一种架构风格,用于开发网络服务。Node.js 是一个基于 JavaScript 运行时的开发平台,可以用于构建高效且可扩展的服务器端应用程序。因此,使用 Node.js 来开发 REST API 是一种常见的选择。 借助 Node.js 的特性,我们可以轻松地构建 REST API。使用 Node.js 的异步非阻塞 I/O 模型,可以提供高度并发的能力,使服务器能够处理大量的请求。此外,Node.js 的事件驱动架构也使得处理请求变得更加高效。 在使用 Node.js 开发 REST API 时,我们可以使用许多流行的框架和库,例如 Express、Koa 或 Hapi。这些框架提供了许多便捷的功能和中间件,帮助我们搭建和组织 REST API 的路由和业务逻辑。 Node.js 还可以与许多数据库进行集成,如 MongoDB、MySQL、PostgreSQL 等。这使得我们可以轻松地与数据库交互,存储和检索数据。同时,我们还可以使用其他第三方库和工具来处理认证和授权、API 文档生成、日志记录等常见的开发任务。 通过结合 Node.js 和 REST API,我们能够构建出灵活、可扩展且高性能的服务器端应用程序。这种架构模式使得不同的客户端可以通过 HTTP 协议与服务器进行通信,从而实现前后端的分离和独立开发。并且,REST API 的设计原则和规范使得不同的团队能够更好地协作开发,提高开发效率。 总而言之,使用 Node.js 来开发 REST API 是一种强大而灵活的方式。借助 Node.js 的优势和丰富的生态系统,我们可以轻松构建出高性能、可扩展且易于维护的 REST API
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值