Languages
[Edit]
EN

Node.js / Express.js - AJAX PATCH request

0 points
Created by:
troya
572

In this article, we would like to show you how to make AJAX PATCH requests with Express.js

1. Express.js PATCH methods example

In this section, we use Express.js to handle PATCH requests.

Note: it is required to add external library to run below example, so use:

npm install express

index.js file:

const express = require('express');

const port = 3000;
const app = express();

app.patch('/examples', (request, response) => {
  const data = request.body; 
  // make partial changes to an existing resource

  response.send('put your changed data here');
});

app.listen(port, () => {
    console.log(`server is listening at http://localhost:${port}`);
});

To run the server use: 

node index.js

Output:

server is listening at http://localhost:3000

2. Pure JavaScript (Vanilla JS) AJAX PATCH request

The following is an example execution of a fetch method that sends PATCH requests.

<!DOCTYPE html>
<html>
<body>
  <script>

    var requestData = 'Some text sent to API ...';

    fetch('/examples', {
        method: 'PATCH',
        headers: {
          'Content-Type': 'text/plain',
        },
        body: requestData
    })
        .then((response) => response.text())
        .then((responseData) => {
      	  console.log('Success: ', responseData);
        })
        .catch((error) => {
            console.error('Error: ', error);
        });
    
  </script>
</body>
</html>

Resources

  1. PATCH request - Wikipedia
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js / Express.js - AJAX requests

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join