Building a web application from scratch can be a daunting task, but with the right tools and guidance, it's definitely achievable.
MERN Stack is a popular technology stack used for building web applications, consisting of MongoDB for database, Express.js for server, React.js for client-side, and Node.js for server-side.
To start building a web application, you need to set up a new project, which involves creating a new directory and initializing a new Node.js project using npm.
In the next section, we'll dive into setting up the project structure, including creating a new MongoDB database and connecting it to our Express.js server.
Getting Started
To get started with a mean stack tutorial, you'll need Node.js and a MongoDB Atlas cluster. Node.js can be downloaded and installed from https://nodejs.org/. To ensure you're using the correct version, execute the command `node -v` in your terminal.
You'll also need a MongoDB Atlas cluster, which can be set up by following the Get Started with Atlas guide. This will give you a free cluster and a connection string that you'll need later.
The tutorial is tested with Node.js version 20.11.1, but you can use any version that works for you. The optional chaining operator (`?`) is used in some expressions to avoid errors when reading nested properties.
Here's a step-by-step guide to get you started:
1. Download and install Node.js from https://nodejs.org/.
2. Set up a MongoDB Atlas cluster by following the Get Started with Atlas guide.
3. Take note of your cluster's connection string, which you'll need later.
By following these steps, you'll have everything you need to start building your mean stack application.
Building a Node.js and Express Application
To start building your Node.js and Express application, create a directory that will host your project and its files, naming it something like "mean-stack-example". This will be the foundation of your application.
You'll be using shell commands to create directories and files throughout the tutorial, but feel free to use any other method if you prefer. Now, let's create the directories and files that will host your server-side application — server and server/src — and also initialize a package.json file for it.
To implement the endpoints for your employees, you'll use the router provided by Express in the file src/employee.routes.ts. The POST /employees endpoint will allow you to create a new employee.
Here are the key dependencies you'll need to install for a basic Express.js server:
- Express.js, a popular Node.js web application framework
- body-parser to parse the request content and headers
- jade (renamed to pug) for templating engine
- cookie-parser to parse cookies
To create a new Express app using the template, use the command express appname, where appname is a custom name for your app. This will generate an express application template that's basic enough to get you started with two basic routes, / and /users.
You'll notice in the package.json file, there are a list of dependencies together with express, which help a lot in building your application.
Express and Node.js
Express and Node.js are the backbone of the MEAN stack, and understanding how they work together is crucial for building robust applications.
You can create a basic Express.js server in a file named `server.js` to get started. This will be your application's backend.
Express.js is a popular Node.js web application framework that's lightweight and easy to use. It's frequently used with a MongoDB database in well-known development stacks like MEAN.
To install Express.js, you'll need to run the command `npm install express` in your terminal. Once installed, you can create a bare minimum application of an Express app from scratch that will serve your built Angular app's index.html file.
Express.js is an excellent choice for creating APIs, and it's easy to customize and provides flexibility. Some of its key features include being lightweight and easy to use, installing and configuring is simple, and it's a good option for communicating with various people.
Here are some key benefits of using Node.js:
- The Node app is 35% quicker than the other apps.
- Require fewer people to build the application.
- Needs fewer lines of code.
- Javascript is used by node.js.
- Transition seamlessly from front-end to full-stack development.
Backend with Express.js and Node.js
To build the backend of our MEAN stack application, we'll use Node.js and Express.js. We'll start by creating a directory for our project, which we'll name mean-stack-example. We'll then create the server-side application in a directory named server and initialize a package.json file for it.
Express.js is a lightweight and easy-to-use framework for building back-end web applications. It's often used with MongoDB databases in popular development stacks like MEAN. Some of its key features include being lightweight, easy to use, and simple to install and configure.
We can create a basic Express.js server in a file named server.js, which will be our application's backend. We can install Express.js using npm and create the server using the following code:
```javascript
const express = require('express');
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello MEAN Stack!');
});
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}/`));
```
This will create a server that runs at http://localhost:3000 and displays "Hello MEAN Stack!" when visited in a browser.
Server-Side Employee Interface
The server-side employee interface is a crucial part of our backend application, and it's where we define the structure of our employee object. We'll be using an Employee interface to ensure that all of our employee data follows a consistent shape.
We've created an Employee interface in our server-side code, specifically in the mean-stack-example/server/src/employee.ts file. This interface defines the structure of the employee object with fields for name, position, and level.
The _id field is optional in our Employee interface because it's generated by MongoDB when we insert a new employee document. This means we don't need to specify it when creating a new employee, but it will be populated when we retrieve an employee object from the database.
We're also using JSON schema validation to ensure that all of our documents follow the shape of our Employee interface. This helps prevent us from accidentally storing data that doesn't match our model, and it's a good practice to follow in our application development.
Backend Setup
To set up the backend with Express.js and Node.js, start by creating a directory to host your project and its files. Name it something like `mean-stack-example` and use shell commands or your preferred method to create directories and files.
You'll need to install Express.js, a popular Node.js web application framework, using npm. This will be your application's backend.
Create a basic Express.js server in a file named `server.js`. This will be your application's backend, and it should be running at `http://localhost:3000`, displaying "Hello MEAN Stack!" when you visit this URL in your browser.
To connect Angular to Express.js, update the `apiUrl` property in the `src/environments/environment.ts` file to point to your Express.js server.
Express.js is a free and open-source framework for back-end web applications, frequently used with a MongoDB database in well-known development stacks like MEAN. It's lightweight and easy to use, with simple installation and configuration.
Here are some key features of Express.js:
- Lightweight and easy to use
- Installing and configuring is simple.
- Easy to customize and provides flexibility.
- The best alternative for creating APIs.
- Communicating with various people is a good option.
Node.js is an environment for running JavaScript code, primarily used in back-end application construction. It's 35% quicker than other apps, requires fewer people to build the application, and needs fewer lines of code.
Here are some key features of Node.js:
- The Node app is 35% quicker than the other apps.
- Require fewer people to build the application.
- Needs fewer lines of code.
- Javascript is used by node.js.
- Transition seamlessly from front-end to full-stack development.
Sources
- https://www.ibm.com/topics/mean-stack
- https://www.mongodb.com/resources/languages/mean-stack-tutorial
- https://codecapsules.io/tutorial/getting-started-with-mean-stack-a-step-by-step-tutorial/
- https://www.softlogicsys.in/mean-stack-tutorial-for-beginners/
- http://www.bradoncode.com/tutorials/learn-mean-stack-tutorial/
- https://medium.com/@onejohi/mean-stack-quick-guide-f50351ba56eb
Featured Images: pexels.com