How to setup express server to start the development using NodeJS, Express, ReactJS, and MongoDB. In this tutorial let’s dive to setting a server to deploy and run the MERN application.
Initialize The project.
- Open the project folder in vs code or any text/code editor
- Open an integrated command line in vs code terminal or command prompt or terminal in your machine
- Open into the current working folder.
Use the following command to initialize the project.
PS D:\work\Codebun\tutorial> npm init Wrote to D:\work\Codebun\tutorial\package.json: { "name": "tutorial", "version": "1.0.0", "description": "", "main": "app.js", "dependencies": {}, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
⇒ After initialization, a file named package.json is auto-generated maintaining all the dependencies you install in the future and keep the record of version project and license too.
What is Express?
Express a node js application framework that is used for the development of web applications and mobile applications. As we use javascript we need to install express as instructed in the introduction of this tutorial using npm.
$ npm install express --save
After this command, the package.json file is updated and dependency is added along with the package name.
=> After installing express, require it in the app.js using the require() function of JavaScript.
=> Make an instance of express that can further be used to call express functions.
=> Use the port variable to assign the port for the project. E.x:- port 3000
=> Using the listen to function we can drive a port to host our project on a server on the localhost.
const express = require("express"); const app = express(); const PORT = 2000; app.listen(PORT,()=>{ console.log(`Listening on port ${PORT}`); }); ⇒Use the following command to run the program and check whether it works or not. PS D:\work\Codebun\tutorial> node app.js Note:-This command is used to start the server and run the node js web app.(remember this command)
OutPut ⇒
PS D:\work\Codebun\tutorial> node app.js Listening on port 2000
Dive in Depth of express
- Express is not just used to start the server it is a very deep framework and very powerful too.
- In the upcoming tutorial, we are gonna learn about various functions and use of express as this tutorial moves forward.
- Talking about express it has 4 basic methods with their work.
- express.json()
- It is built-in middleware to parse the incoming data in JSON format and based on body-parser.
- express.static()
- It is used to load static files to servers like custom CSS or javascript files for frontend.
- express.Router()
- It is used to create new router objects. Used in the Routers folder of MVC workflow(Workflow and Folder Structure Tutorial).
- express.urlencoded()
- It is an express middleware function. It parses the incoming request to URL-encoded payload and is based on the body-parser.
- express.json()
- Express instances are also used to create Different types of requests like getting, post, delete, etc…
- It also carries different functions for the callback parameter request and response.
- In-depth all this will be covered in the upcoming tutorial when the use of these functions will be encountered.