We'll be building the website using the MEN Stack framework. The M in MEN Stack represent the database tool we'll be using -- MongoDB. The E in MEN Stack represent the backend framework we'll be using -- Express. And finally, the N in MEN Stack represent the software that servers the web server -- Node.js. Node.js accepts requests from the user and sends the content to the browser. For a more detailed explanation, refer to the following link.
The following module goes through the installation of MongoDB, Node.js, and Express. After the installation, this module goes through an example of an MEN Stack web server.
Requirements:
Computer with Internet access
Learning Objective:
Set up MEN Stack Webserver
Install and set-up MongoDB
Install and setup Node Package Manager (NPM)
Install Node.js
Install Express
MongoDB is a document database that belongs to a family of databases called NoSQL -- not only SQL. In MongoDB, records are documents that behave a lot like JSON objects in JavaScript. Values in documents can be looked up by their field’s key. Documents can have some field/keys and not others, which makes Mongo extremely flexible.
Depending on the operating system (OS), installing MongoDB varies. Refer to the appropriate tutorial based on the operating system.
1. Make sure Homebrew in installed. Refer to the Homebrew page for installation.
Copy-paste the installation command onto the Terminal app and run the command. Follow the instruction from thereon until Homebrew finish installing.
2. Once Homebrew is installed, on the Terminal app run the following command: brew update
Note: This may take a few minutes to run.
3. Uninstall old version of MongoDB:
In the Terminal app, run the following command: brew services stop mongodb
Run: brew uninstall mongodb
4. Installing MongoDB:
In the Terminal app, run the following command: brew tap mongodb/brew
Run: brew install mongodb-community
Run: brew services start mongodb-community
5. Check to see if MongoDB is installed:
In the Terminal app, run the following command: mongo --version
The following instructions assums Ununtu as the Operating System, alongside apt-get as the package management system. For a different package manager or operating system, refer to the note below and install MongoDB from the installation page.
1. Open the Terminal app, and run: sudo apt-get update
2. Run: sudo apt-get upgrade
3. To Install MongoDB, run: sudo apt-get install -y mongodb
If the above command doesn't work, try the following: sudo apt-get install -y mongodb-org
4. Check to see if MongoDB is installed: mongo --version
1. Install the MongoDB Community Edition through MongoDB downloads center. Start by clicking on MongoDB Download Center.
2. Select the appropriate OS and install the MSI package with the newest version.
Warning: Do NOT install the .zip file.
3. Once the download is competed, click on it to initiate installation of MongoDB.
Once the setup wizard starts, click Next.
Accept the license agreement and click Next.
We'll be performing the Complete installation.
On the Service Configuration page, everything can be left as default. Click Next.
Installing the MongoDB Compass is optional. Click Next.
Start installation by clicking Install.
4. Check to ensure MongoDB is properly installed. One of the only ways to do this is to check the log files for the following line:
[initandlisten] waiting for connections on port 27017
The default path for the log file usually is:
C:\Program Files\MongoDB\Server\4.2\log
If the default path doesn't work, locate the directory in which MongoDB is installed.
Open the mongod text document and towards the end of the file, look for aforementioned line.
Depending on the OS, installing Node.js varies. Refer to the appropriate tutorial based on the operating system.
1. On the Terminal app, run the following command: brew install node
2. Check to see if node is properly installed, run the following command to check its current version: node -v
3. The Node Package Manager (npm) comes pre-installed with node. Check to see it's current version: npm -v
1. Open the Terminal app and run the following command: sudo apt-get update
2. Run: sudo apt-get upgrade
3. To install Node.js, run: sudo apt-get install -y nodejs npm
4. Once node and npm installation is completed, check to see it's installation version:
Run: node -v
Run: npm -v
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
Once the above command finish running, run the following command: sudo apt-get install -y nodejs
Verify that the node verison is at least 10.x
1. Install Node.js v10.0.0 from its website. Other version of Node.js hasn't been tested. Download at you own risk.
1.1. Make sure to install MSI file. Do NOT install any other extension. Make sure to install the correct version based on the operating system. x64 represent 64-bit architecture while x86 represent 32-bit architecture.
2. Once the download is completed, click on the .msi file to start installation. It may ask for your permission to download the software. Accept the request to download the software.
Click Next on the Node.js Setup Wizard.
Accept the License Agreement and click Next.
Feel free to change the installation path, otherwise, leave it as default. Click Next.
Under the Custom Setup page, select Add to PATH icon and click Next.
Check the permission to automatically install the necessary tools. Click Next.
Start installation by clicking Install.
Once the Node.js Setup Wizard is done installing, click Finish. This will open up the Command Prompt to install any additional tools that's necessary. If Command Prompt doesn't open, skip to the last step of installing Node.js, which is checking the version of the Node.js installation.
Press any key to continue installation of Node.js.
Once the Command Prompt closes, the installer will open Windows PowerShell to continue the installation process.
Note: it may seem like the installer is frozen. It's NOT. The installation can take upwards of 25 - 30 min or longer, depending on what's already pre-installed on the computer and on the internet download speed. You may need to restart the computer once the installation is completed.
Verify that the appropriate version of Node.js is installed by opening another window of Windows PowerShell and running the following command:
node -v
npm -v
Note: The Node Package Manager (npm) comes pre-installed with node.
Express runs on top of Node. Therefore, if Node is installed on the computer then installing Express is extremely easy. However, unlike Node, Express isn't installed globally for the entire computer. We have to install Express for each project separately.
1. Let us start by creating a project. Open the Terminal app on MacOSX or Linux, or PowerShell on Windows. Navigate to the directory where you want to create your project.
For Windows user only: make sure to Run as Administrator when opening PowerShell. Additionally, in all of the commands that follow, the sudo keyword is be dropped, as the keyword is reserved only in a Unix-like operating system.
2. Let's create the project directory using the mkdir command: mkdir intro_to_iot
3. Enter into the project folder using the cd command: cd intro_to_iot
4. When making a node web app, it's common to practice to follow the next step so that your web app is portable across multiple platform. Let's start creating the node app using the following command inside the project directory:
npm init
This will prompt us to answer several question. To keep the default suggested response (written inside the parentheses), simple press the enter key. To input a different response, simple type the desire response and press the enter key.
5. Keep the package name, version, and description as default.
6. As for entry point, we'll use app.js instead of index.js. We'll keep the test command, git repository and keywords as default. As for author, give it an appropriate author name. Everything is kept as default.
7. Once the initialization steps are completed, a file titled package.json should appear. This package.json will store all the important information about the project.
8. We're now ready to install Express. We'll use the npm command to install express and save it to the package.json. Use the following command to install express: sudo npm install --save express
8.1. Once express is finished installing, one folder called node_modules and a file called package-lock.json should have been created. Don't worry about the purpose of package-lock.json. However, the directory node_modules contains all the dependencies for the project, including express. Additionally, if we look at package.json, it should contain a dependencies for express.
In addition to using Express, we'll also be relying on the following packages that needs to be installed. All of these installation is done in the same folder where Express is installed.
For Windows user only: make sure to Run as Administrator when opening PowerShell. Additionally, in all of the commands that follow, the sudo keyword is be dropped, as the keyword is reserved only in Unix-like operating system.
In the same project folder where Express is installed, we'll install EJS using the following command:
sudo npm install --save ejs
In the same folder where Express is installed, we'll install body-parser using the following command:
sudo npm install --save body-parser
In the same folder where Express is installed, we'll install dotenv using the following command: sudo npm install --save dotenv
In the same folder where Express is installed, we'll install Mongoose using the following command:
sudo npm install --save mongoose
In the same folder where Express is installed, we'll install Mongoose using the following command:
sudo npm install --save gulp@3.9.1
In the same folder where Express is installed, we'll install Semantic-Ui using the following command:
sudo npm install --save semantic-ui
On the Semantic-Ui set-up, we'll select Automatic. Press Enter once selected.
On the next step, verify the project folder and select Yes. Press Enter.
Next, it's asking from the location of Semantic-Ui inside the project folder. We'll keep the location as default (semantic/). Press Enter.
This should start the installation process. At the end of the installation, there may be a warning message indicating vulnerabilities. Feel free to ignore those warnings.
If installed correctly, there should be a semantic folder and a semantic.json file inside the project directory. We'll be moving this folder around once we start building our web application.
Change into the semantic folder using the following command:
cd semantic
Build the gulp file using the following command: gulp build
For Windows user only: if there's a
gulp: The term 'gulp' is not recognized as the name of a cmdlet,....
error (refer to the image on the left), run the following command:
npm install -g gulp-cli
Once finished, rerun: gulp build
Use the following commands to install a version of node that's compatible:
npm install -g n
sudo n 11.15.0
npm install
npm rebuild node-sass
Re-run the installation of Semantic-Ui once again one the above steps are completed.
Uninstall the current version of Node.js.
To uninstall Node.js, open Control Panel and click on Uninstall a program.
Locate Node.js and click on Uninstall.
Once Node.js is uninstalled, use the following link to install a version of node that's compatible. Node.js v10.0.0 should be compatible. Make sure to install MSI file. Do NOT install any other extension. Make sure to install the correct version based on the operating system. x64 represent 64-bit architecture while x86 represent 32-bit architecture.
Once Node.js in installed, retry installing Semantic-Ui once again.
By now, the following should be completed:
Installed MongoDB
Installed Node.js
Installed Express in the Project folder
Installed EJS
Installed Body-Parser
Installed Mongoose
Installed dotenv
Installed gulp
Installed Semantic-Ui
One quick way to verify all the installation is to check the node_modules directory for all the dependencies.