How to handle all request in express js - deesoft service

How to handle all request in express js

Deepak Tailor Image
Deepak Tailor - Nov 24 2021
How to handle all request in express js

Role of routing is very important topic in nodejs. Many types of methods are given in nodejs for route like as (GET, POST, PUT, DELETE).

1. Get

2. Post

3. Put

4. Delete

5. Many more.

Get method for all get request handle

Post method for all post request handle

Put method for all put request handle

Delete method for all delete request handle

Four types of methods are most commonly used to create REST APIs.

Create new file router.js

1. Import express module

2. Call router function in express module

import express from 'express';

// call router function
const router = express.Router();
create get request
// set routing
router.get('/', (req, res, next) => {
    console.log('get request');
});

router.get('/fetchAllStudents', (req, res, next) => {
    console.log('fetch all student request');
});

router.get('/singleStudentDetails/1', (req, res, next) => {
    console.log('single student details request');
});
create post request
router.post('/', (req, res, next) => {
    console.log('post request');
});

router.post('/submitForm', (req, res, next) => {
    console.log('submit form request');
});
create put request
router.put('/', (req, res, next) => {
    console.log('put request');
});

router.put('/updateAllStudents', (req, res, next) => {
    console.log('update student request');
});

router.put('/updateSingleStudent/1', (req, res, next) => {
    console.log('update single student request');
});
create delete request
router.delete('/', (req, res, next) => {
    console.log('delete request');
});
router.delete('/deleteAllStudents', (req, res, next) => {
    console.log('delete all student request');
});

router.delete('/deleteSingleStudent/1', (req, res, next) => {
    console.log('delete single student request');
});

Export this file when route is complete.

import router from './routes';
// router use
app.use(router);

To connect, import the router file into the server file.

Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in