The MEAN stack is a collection of technologies that are commonly used together to build web applications. The acronym stands for MongoDB, Express, AngularJS, and Node.js. These technologies are all open-source and work well together to create a full-stack JavaScript solution. In this tutorial, we will walk through the process of building a RESTful API using the MEAN stack.
Setting up the project
The first step in building our API is to set up the project. We will use the AngularJS generator to scaffold out the basic structure of our application. In your command line, navigate to the directory where you want to create your project and run the following command:
npm install -g generator-angular
This will install the AngularJS generator globally on your machine. Next, run the following command to create the basic structure of your project:
yo angular my-api
This will create a new directory called “my-api” in your current directory. Navigate into this directory and run the following command to start the development server:
grunt serve
This will start the development server and open your application in the browser. At this point, you should see the default AngularJS welcome page.
Installing MongoDB
The next step is to install MongoDB. MongoDB is a NoSQL database that is commonly used with the MEAN stack. It is a document-oriented database which means that it stores data in a format that is similar to JSON. To install MongoDB, navigate to the MongoDB website and download the appropriate version for your operating system. Once you have installed MongoDB, you can start the MongoDB server by running the following command:
mongod
his will start the MongoDB server on your machine.
Creating a Mongoose model
Now that we have MongoDB installed and running, we can start creating our Mongoose model. Mongoose is an Object-Document Mapping (ODM) library that is commonly used with MongoDB and Node.js. It allows us to define our data model and interact with the database using JavaScript. To install Mongoose, run the following command in your project directory:
npm install mongoose --save
Once you have installed Mongoose, you can create a new file in your project called “model.js” and define your data model. Here is an example of a simple data model for a “Task” object:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TaskSchema = new Schema({
name: String,
description: String,
status: {
type: String,
default: 'pending'
}
});
module.exports = mongoose.model('Task', TaskSchema);
In the example above, we are defining a “Task” object with three properties: “name”, “description”, and “status”. The “name” and “description” properties are strings, and the “status” property is a string with a default value of “pending”.
Implementing the RESTful API
Now that we have our MEAN stack set up and running, we can start building our RESTful API. In this section, we will create the routes and logic for our API using Express.js, which is the web application framework that comes with Node.js.
First, we will create a new file in the root of our project called api.js
. This file will hold all of the routes and logic for our API.
In api.js
, we will start by importing the necessary modules and connecting to our MongoDB database using Mongoose. We will also create a new Express.js router to handle all of our API routes.
const express = require('express');
const mongoose = require('mongoose');
const router = express.Router();
mongoose.connect('mongodb://localhost/mydatabase');
Next, we will define our routes for our API. In this example, we will create routes for creating and retrieving items from our database.
// Create item
router.post('/items', (req, res) => {
// Create new item in database
});
// Get all items
router.get('/items', (req, res) => {
// Retrieve all items from database
});
Now, we will add the logic for creating and retrieving items from our database. In this example, we will assume we have a Mongoose model called Item
that represents our items collection in the database.
// Create item
router.post('/items', (req, res) => {
// Create new item in database
Item.create(req.body, (err, item) => {
if (err) return res.status(500).send(err);
return res.status(201).send(item);
});
});
// Get all items
router.get('/items', (req, res) => {
// Retrieve all items from database
Item.find((err, items) => {
if (err) return res.status(500).send(err);
return res.status(200).send(items);
});
});
Finally, we will export our router so that it can be used in our main app.js
file.
module.exports = router;
In the app.js
file, we will import the api.js
file and tell Express.js to use it for all routes starting with /api
.
const api = require('./api');
app.use('/api', api);
With this setup, our RESTful API is now complete and ready to handle requests. By using the MEAN stack, we were able to quickly and easily create a powerful, full-featured API that can handle CRUD operations and connect to a MongoDB database.