Express Install , MVC architecture concept , Nodemon install , express server configuration , Postman Install , basic get() , post() operations in api and testing using postman.

 Express Install , MVC architecture concept , Nodemon install , express server configuration , Postman Install , basic get() , post() operations in api and testing using postman.


Here's a basic guide to get you started on your first day with Express:


 1. Express Installation

   - Step 1: Make sure Node.js is installed on your system. You can check this by running:

     ```bash

     node -v

     npm -v

     ```

   - Step 2: Initialize a new Node.js project by running:

     ```bash

     npm init -y

     ```

   - Step 3: Install Express using npm:

     ```bash

     npm install express --save

     ```


 2. MVC Architecture Concept

   - Model: Handles the data logic (e.g., database interaction).

   - View: Manages the display of data (e.g., HTML, templates).

   - Controller: Handles the business logic, processes requests, and returns responses.

   - Express is often used with MVC to separate concerns, making the code more organized and maintainable.


 3. Nodemon Installation

   - Nodemon automatically restarts the server when file changes in the directory are detected.

   - Install Nodemon globally:

     ```bash

     npm install -g nodemon

     ```

   - Alternatively, you can install it as a dev dependency:

     ```bash

     npm install --save-dev nodemon

     ```


 4. Express Server Configuration

   - Create a basic Express server:

     ```javascript

     const express = require('express');

     const app = express();

     const port = 3000;


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

       res.send('Hello, World!');

     });


     app.listen(port, () => {

       console.log(`Server is running on http://localhost:${port}`);

     });

     ```

   - Run the server using:

     ```bash

     node app.js

     ```

   - Or, using Nodemon:

     ```bash

     nodemon app.js

     ```


 5. Postman Installation

   - Download and install Postman from [here](https://www.postman.com/downloads/).

   - Postman is a tool for testing APIs by sending HTTP requests to your server.


 6. Basic `get()` and `post()` Operations

   - GET Request:

     ```javascript

     app.get('/api/data', (req, res) => {

       res.json({ message: 'GET request received!' });

     });

     ```

   - POST Request:

     ```javascript

     app.use(express.json());


     app.post('/api/data', (req, res) => {

       const data = req.body;

       res.json({ message: 'POST request received!', data });

     });

     ```


 7. Testing Using Postman

   - Open Postman and create a new request.

   - For a GET request:

     - Select `GET` method, enter `http://localhost:3000/api/data`, and click `Send`.

   - For a POST request:

     - Select `POST` method, enter `http://localhost:3000/api/data`, go to the `Body` tab, select `raw`, and choose `JSON` as the format.

     - Enter some JSON data (e.g., `{"name": "John"}`) and click `Send`.


This should give you a solid foundation to start working with Express.



------------------------------------------------------------------------------------------------------

server.js

const express=require('express');

//object create 

const app=express();

const port=4000; 

//configuration code for accepting json data / form data from ui
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.get('/',(req,res)=>{
   res.send("<h3>Welcome to my Express Application and Coding and be Happy</h3>");
})

app.get('/test1',(req,res)=>{
    res.send("<ul><li>hello kolkata</li><li>hello pune</li></ul>");
}) 

app.get('/test2',(req,res)=>{
     let emp={id:10,nm:'raj',age:33,city:['pune','goa','punjab']};
     res.json(emp);
})

app.post('/insert',(req,res)=>{
      let data=req.body;
      console.log(data);
      res.status(200).json({msg:"thank you data received"});
})

app.post('/login',(req,res)=>{
     let data=req.body;
     if(data.uid=="sourav07" && data.pwd=="1234")
     {
        res.status(200).json({msg:"user is valid"});
     }
     else 
     {
        res.status(200).json({msg:"user is invalid"});
     }
})

app.get('/edit/:uid',(req,res)=>{
    let id=req.params.uid;
    console.log("Required id :"+id);
    res.status(200).json({msg:"id received"});
})

app.listen(port,()=>{
    console.log(`server is running in the port ${port}`);
})

Comments

Popular posts from this blog

Password Encryption in Express using bcryptjs module , Password Decryption in Express using bcryptjs module. Login API using Express with password encryption . Example . Testing with postman

Routing concept in Express , Controller Concept in Express , Get() , Post() routing in express , Testing in postman. Mongoose (ODM) install , Connectivity with Mongodb using mongoose.Connect() . Examples