NPM Packages: To use or not to use?
JavaScript evolves overtime. Sometimes packages are no longer supported and updated because they utilize an old JavaScript syntax, or the package is no longer popular because there is a better way to approach a problem. A package is marked as depreciated when it is no longer supported. The request.js package is an example of a depreciated package. The request.js package is interesting because HTTP requests allow JavaScript applications to communicate in real-time using the HTTP protocol to create rich-media applications, games and utilities. HTTP requests made with request.js provide simple, easy to use syntax with the use of a callback function:
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.error('error:', error); // handle error
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
The use of request.js is not practical as it may not be optimized to run effectively, and may contain bugs as well as security vulnerabilities. Because request.js is no longer supported, its only purpose is to support legacy software that depended on this module when it was still supported. Instead of using request.js to make a request to a third party api, you could use the axios.js package to make HTTP requests. HTTP requests made with axios.js provide a flexible syntax with the use of a promise chain:
const axios = require('axios');
axios.get(’http://www.google.com’)
.then(function (response) { // handle success
console.log(response);
})
.catch(function (error) { // handle error
console.log(error);
})
.finally(function () { // always executed
});
When researching packages, it is important to check if a package currently contains active support, bugs, and security vulnerabilities. Even though request.js was designed to provide a simple, convenient HTTP call, axios.js has surpassed it in popularity and it proves easier to use because it is currently supported and widely used.
References
Axios. npm. (n.d.). Retrieved April 26, 2023, from https://www.npmjs.com/package/axios
D. D. A. (2023, January 6). Why you should always pass objects as function parameters in JavaScript. Medium. Retrieved April 26, 2023, from https://medium.com/coding-at-dawn/why-you-should-always-pass-objects-as-function-parameters-in-javascript-7fb7c5833dc6
Request. npm. (n.d.). Retrieved April 26, 2023, from https://www.npmjs.com/package/request
Comments
Post a Comment