azad education azad education

The next-generation blog, news about technology.

India (HQ)

[email protected]

Build Node.js rest api with express and MySQL in MVC pattern

azad education
Azad Education Dec 02, 2022 · 5 min read
Build Node.js rest api with express and MySQL in MVC pattern

Build Node.js Rest API in MVC pattern with Express and MySQL

What is Node.js

Node.js is an open-source server environment programing language. It's free to use for any platform like Windows, Linux, Unix, and Mac. Node.js is trending for server-side scripting tasks. Node.js is very popular nowadays because it uses JavaScript on the server and uses asynchronous programming.

What we will learn in this blog?

  1. Installation of Node.js on Windows and Ubuntu.
  2. Create a Node.js project.
  3. Install Express js, MySQL and why use it?
  4. Our Node.js project file structure.
  5. Creating a web server in the express.
  6. Install Nodemon and why it is too useful?

 

1. Installation of Node.js on Windows and Ubuntu (Follow these steps, closely).

        In Windows follow these steps.

  • http://nodejs.org/download/ download the 64 bits version, 32 is for hipsters
  • Install it anywhere you want, by default: C:\Program Files\nodejs
  • Control Panel -> System -> Advanced system settings -> Environment Variables
  • For the first-time installation, we have the "Add to PATH" option. By default, it is selected. Make sure that you leave it checked.

       Ubuntu Lover follows these steps.

  • I love to use ubuntu because no need to follow any extra rules. Only run this command to install Node.js in Ubuntu.
    sudo apt-get install nodejs

     Note:- npm is automatically installed with Node.js in the latest version of Node.js.

Now we will check both Node.js and NPM are installed or not.

Both operating systems to check we will run this command in the terminal.

For Node.js version check node --version and also for windows.

For the NPM version check npm --version both for Ubuntu and windows.

2. Create a Node.js project.

Create a new folder and open the terminal from this directory. 

To create a Node.js package.json for storing project settings and information.

Type this command in the terminal npm init -y this command will create a package.json file.

{"name": "node-js","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC","dependencies": {}}

Now we will create a new index.js file that should be available in the package.json in the main object key. 

3. Install Express js and why use it?

Express js is a server framework of node js. It is so popular framework because has various features. Like

  1. Dynamic routing
  2. In Node js compare Express js server rending is too fast.
  3.  Send HTTP requests.
  4. Body Parsing.
  5. File read and edit.

Install Express js framework globally.

npm install express --save

4. Install MySQL and why use it?

We are installing MySQL in the node js application that we can run the MySQL query.

npm install mysql2

5. Our Node.js Project files and folder structure.

node js folder structure azad

5. Now we need to install the Body parser express js package.

Body parser will parse our post request raw data in JSON format.

npm i body-parser --save

5. Creating a web server in the express and Index.js file code. (For more detail we are creating a youtube video)

const express = require("express");

const bodyParser = require("body-parser");

const app = express();

const port = 3000;

require("dotenv").config();

const env = process.env;

const router = require("./route/routes");

 

/****************************************************/

app.use(bodyParser.json());

app.use(

  bodyParser.urlencoded({

    extended: true,

  })

);

/**************************************************** */

app.get("/", (req, res) => {

  res.json({ message: env.host });

});

app.use("/request", router);

/*******************************************************/

app.listen(port, () => {

  console.log(`Example app listening at http://localhost:${port}`);

});

When our Server will be start so we check in the console log (Terminal not of the browser).

This will print http://localhost:3000 so our index.js file code is completed.

 

avatar

Azad Mohammed

An editor at AzadEducation
View Articles

I'am Azad, These blogs, tech skills and programing news, I am sharing with my exprience. I have 4+ year experiece in the web development. Also we are learning investing ideas. I believe in "big mind big dream", that will convert in smart mind. You can follow me on Linkedin profile.

Categories
C#
10
Java Script
8
SEO
4
Quick Hack Solution Tips
4

Subscribe to our mailing list!

We don't spam