Then the save method of the model stores the modified data in to the database. We call Lodash's extend method in order to modify the feed's properties with the ones that came from the user. If the document is created successfully, we return the new document to the caller. Upon error, we return to the signup form since most likely the user is not logged in. Finally, we call the model's save method to store the document in the database. Then we add to this variable the reference to the current logged in user to be stored. In order to create a feed we implement the create() function: /**įirst, we create a feed variable based on the Feeds model. First, don't forget to rename the app/controllers/articles.js file to app/controllers/feeds.js. You should be noticing a pattern by this point. Let's take a look at them one by one and see the implementation details. ![]() ![]() All the necessary functions for the CRUD operations we perform are implemented in the controller. The controller is responsible for implementing the functions called by the router, when a specific route is requested. Notice that we don't handle the creation of an id for our documents here, since this is taken care of for us by the MongoDB system.įinally, we define a load() function that filters the documents in the database based on the id of the current logged in user. We define our database structure here as follows: we will have a feeds table containing a feedUrl string type field and a reference to a user which will store the id of the current logged in user. Var hasAuthorization = function(req, res, next) ).populate('user', 'name username').exec(cb) Var authorization = require('./middlewares/authorization') Var feeds = require('./controllers/feeds') Then modify it as shown below: // app/routes/feeds.js First, change its name from articles.js to feeds.js. So, we have to modify it a little to fit our needs. If we look at its contents, we see that this is what we want to have, but applied to the articles. Now, let's open the file app/routes/articles.js. The picture below shows what we expect to have: Let's spend a minute and think about what routes our app should to respond to. After all, this is why we are using boilerplate code, to modify and adapt it to the needs of our application. We will modify this API to respond to our need of handling the feed URLs. Luckily for us, the MEAN stack gives us such an API for handling the articles that can be created. We will implement a REST API to respond to the user's requests for viewing, creating, deleting and modifying feeds. In this first part, we will discuss the implementation of the server part of our application (the backend). The user will also have the possibility to filter the feeds on various criteria. ![]() The last feeds are displayed to the user on the main page and they will show the article's title, an excerpt and allow the user to click on the title to read the entire post. The feed's URLs are stored in a Mongo database on the server so that they are available even after logging off, or after closing the browser. As we saw in the previous article, the stack comes with a full authentication mechanism already implemented, so we can use it without further modifications. The user has to authenticate in order to have access to the application, so that the feeds of one user are not visible to others and most importantly, to users that are not logged in. The application will allow its user to manage their list of feeds by adding a new feed, deleting an existing one, modifying an existing one and, of course, seeing a list of all the feeds in which the user is interested in. We will be building an RSS feed reader using the MEAN stack. Now it's time for some actual coding! What We'll Be Building The previous post covered the installation of the MEAN stack and also presented what we ended up with in terms of its directory structure, after installation. This article is a continuation of Introduction to the MEAN Stack.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |