In: Computer Science
Describe the components of the MEAN architecture and what role each component plays in the architecture of a web application. Describe the supporting cast components.
MEAN is a free and open-source JavaScript software stack for building robust, fast, and maintainable dynamic websites and web applications.
MEAN is an acronym for MongoDB, ExpressJS, AngularJS, and Node.js. From client to server to database, MEAN is full-stack JavaScript. It is very simple to use front-end and back-end and also written in one language for both client-side and server-side application execution.
Components of MEAN architecture are:
M- Mongo DB
MongoDB is a document database in which the data is stored in flexible, JSON-like documents.
E- Express JS
A- Angular JS
N- Node JS
These are the following features of MEAN stack architecture:
After installing the Angular CLI, we will create a new Angular project with the CLI, and then we will add MongoDB, Node, and Express to that project later. First, we will navigate into that folder where we want to create the project, and then we will create a project using the ng new command in the following way: C:\Users\snehita\OneDrive\Desktop\Mean Stack> ng new first-app
After creating the project successfully, we will navigate to the project folder and run the following command C:\Users\snehita\OneDrive\Desktop\Mean Stack\first-app>ng serve
Now, we want an IDE or some advanced text editor to make change some changes in our application. These advanced text editor makes working with our code much easier. We will install the visual studio IDE because it is free, pretty powerful, and highly extensible. It is available for Windows but also for Mac and Linux.
After downloading the installer, we will run it and install it. After installing it, we will open it. We will go to the File menu and open our first-app project from there. Now, we first need to install a certain extension. So, we will go to the view menu and click on the extensions. We will search for Angular Essential and install it in our IDE. The Angular Essential is an amazing collection of different extensions that enhance the IDE to work. We will also search for the Material Icon Theme.We have set our Angular, Node and IDE successfully
We will see a bunch of sub-folders and files in our project. Most of these files are completely for the configuration, and there is no need to worry about them.
8. We remove this complete code and write the following line of code. As soon as we replace the code, the page will automatically recompile and reload. It is required that our ng server is running in our machine.
<h1> My First Application </h1>
9. We create a Single Page Application and see that a single page in the index.html is present in the app folder.
This index.html file is served by the ng serve right now. This file doesn't contain a lot of content because our build workflow injects the script imports.
If we go back to our application, we can see that what we saw on the page is the content of the app.component.html file. In this file, we will build our components or html elements. In the index.html file, we see one html element in the body section. It is not a default html element; it is our custom component.
10. In the end, it detects this app-root element and swaps it with the content of our component.
There are other essential file name app.module.ts. This file is important for angular. Angular thinks in applications, and applications are split up in modules.
In this project, there is one module, and that module defines the building block of our applications. Components are not the only but probably the most important building block of an angular application. In the app.module.ts file, there will be a ngModule in which our AppComponent is declared. This is registering with angular. So, now angular is aware of the AppComponent. It alone would only allow us to use that component selector in another angular component, not in the index.html file. This is allowed by adding it to bootstrap array too.
There is another file name main.ts file. This file is executed first.
Adding new component in MEAN stack:
Now, we will create another sub-folder for organizing the files of a single component in it. You can also use a flatter folder structure, and you don't need to use that many folders, it is up to you. We will go with the sub-folder way in our project.
Now, in this folder, we will create a post-create.component.ts We can give any name to this file, but it is a convention in angular to include .component in component files, and .ts is an extension because we use typescript.
We can define the html template in the typescript file, but since we have a bit of a more complex template. So, it is good to create a separate file. We will create an html file name post-create.component.html.
n our typescript file, we create a component by simply creating a new class. A class is a typescript feature and available in the latest JavaScript versions also. It allows us to create a blueprint for an object. Though, we will never create that component object on our own. We just give it to angular, and angular will instantiate it, create it, and use it.
We can only define how such a component would look like, and that includes a name for it.
import { Component } from '@angular/core';
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html',
})
export class PostCreateComponent {
}
We also want to add something to the template. We will write the html code in our post-create.component.html
Now, we will use it in our app.component.html file. We will go to that file and call our component using the selector.
If we save it and run our application, we will see a blank screen. It will show us an error message "app-post-create is not a known element". We have to explicitly register a component if we want to use it. We will do this in the app.module.ts file. So, we will import our component and add it into the declaration as:
import { PostCreateComponent } from './posts/post-create/post-create.component';
declarations: [
AppComponent,
PostCreateComponent
],
Now, we save it and can see the "post create component" in the browser.