Request support for Node.js

Introduction

This page explains the support of the request library; and its extensions: request-promise , request-promise-native , request-promise-any .

Objects

The following specific objects are displayed in CAST Enlighten:

Icon Description
NodeJS Get Http Request Service
NodeJS Post Http Request Service
NodeJS Put Http Request Service
NodeJS Delete Http Request Service

Creating HTTP requests on the front-end

const request = require('request');

// GET request to fetch users
function getUsers(){
	request.get('api/users')
		.then(user => {
			console.log('GET User:', user);
		})
		.catch(error => {
			console.error('Error fetching user:', error);
    });
}

// POST request to add a new user
function addUser(){
	request.post({
		uri: 'api/users',
		body: { name: 'John' },
		json: true
	})
		.then(newUser => {
			console.log('POST New User:', newUser);
		})
		.catch(error => {
			console.error('Error adding new user:', error);
    });
}

// PUT request to update an existing user
function updateUser(){
	request.put({
		uri: '/api/users/1',
		body: { id: 1, name: 'Eve' },
		json: true
	})
		.then(updatedUser => {
			console.log('PUT Updated User:', updatedUser);
		})
		.catch(error => {
			console.error('Error updating user:', error);
    });
}

// DELETE request to delete a user
function deleteUser(){
	request.del('/api/users/2')
    .then(() => {
        console.log('DELETE User');
    })
    .catch(error => {
        console.error('Error deleting user:', error);
    });
}

The code above (alongside with a corresponding back-end side) will create the links below:

The results for the extensions of request mentioned earlier remain consistent.