• Complain

Kolce - SitePoint: Modern JavaScript Tools & Skills

Here you can read online Kolce - SitePoint: Modern JavaScript Tools & Skills full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: SitePoint, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Kolce SitePoint: Modern JavaScript Tools & Skills
  • Book:
    SitePoint: Modern JavaScript Tools & Skills
  • Author:
  • Publisher:
    SitePoint
  • Genre:
  • Year:
    2018
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

SitePoint: Modern JavaScript Tools & Skills: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "SitePoint: Modern JavaScript Tools & Skills" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Theres no doubt that the JavaScript ecosystem changes fast. Not only are new tools and frameworks introduced and developed at a rapid rate, the language itself has undergone big changes with the introduction of ES2015 (aka ES6). Understandably, many articles have been written complaining about how difficult it is to learn modern JavaScript development these days. Were aiming to minimize that confusion with this set of books on modern JavaScript. This book presents six complete JavaScript projects; each taking advantage of modern JavaScript and its ecosystem. Youll learn to build several different apps, and along the way youll pick up a ton of useful advice, tips, and techniques. It contains: Build a Full-Sphere 3D Image Gallery with React VR by Michaela Lehr Build a WebRTC Video Chat Application with SimpleWebRTC by Michael Wanyoike Build a JavaScript Single Page App Without a Framework by Michael Wanyoike Build a To-do List with Hyperapp, the 1KB JS Micro-framework by Darren Jones Use Parcel to Bundle a Hyperapp App & Deploy to GitHub Pages by Darren Jones Interactive Data Visualization with Modern JavaScript and D3 by Adam Janes This book is for all front-end developers who wish to improve their JavaScript skills. Youll need to be familiar with HTML and CSS and have a reasonable level of understanding of JavaScript in order to follow the discussion.

Kolce: author's other books


Who wrote SitePoint: Modern JavaScript Tools & Skills? Find out the surname, the name of the author of the book and a list of all author's works by series.

SitePoint: Modern JavaScript Tools & Skills — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "SitePoint: Modern JavaScript Tools & Skills" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Chapter 6: Introducing Axios, a Popular, Promise-based HTTP Client
by Nilson Jacques

Axios is a popular, promise-based HTTP client that sports an easy-to-use API and can be used in both the browser and Node.js.

Making HTTP requests to fetch or save data is one of the most common tasks a client-side JavaScript application will need to do. Third-party libraries especially jQuery have long been a popular way to interact with the more verbose browser APIs, and abstract away any cross-browser differences.

As people move away from jQuery in favor of improved native DOM APIs, or front-end UI libraries like React and Vue.js, including it purely for its $.ajax functionality makes less sense.

Lets take a look at how to get started using Axios in your code, and see some of the features that contribute to its popularity among JavaScript developers.

Axios vs Fetch

As youre probably aware, modern browsers ship with the newer Fetch API built in, so why not just use that? There are several differences between the two that many feel gives Axios the edge.

One such difference is in how the two libraries treat HTTP error codes. When using Fetch, if the server returns a 4xx or 5xx series error, your catch() callback wont be triggered and it is down to the developer to check the response status code to determine if the request was successful. Axios, on the other hand, will reject the request promise if one of these status codes is returned.

Another small difference, which often trips up developers new to the API, is that Fetch doesnt automatically send cookies back to the server when making a request. Its necessary to explicitly pass an option for them to be included. Axios has your back here.

One difference that may end up being a show-stopper for some is progress updates on uploads/downloads. As Axios is built on top of the older XHR API, youre able to register callback functions for onUploadProgress and onDownloadProgress to display the percentage complete in your apps UI. Currently, Fetch has no support for doing this.

Lastly, Axios can be used in both the browser and Node.js. This facilitates sharing JavaScript code between the browser and the back end or doing server-side rendering of your front-end apps.

Fetch API for Node

there are versions of the Fetch API available for Node but, in my opinion, the other features Axios provides give it the edge.

Installing

As you might expect, the most common way to install Axios is via the npm package manager:

npm i axios

and include it in your code where needed:

// ES2015 style importimport axios from 'axios';// Node.js style requireconst axios = require('axios');

If youre not using some kind of module bundler (e.g. webpack), then you can always pull in the library from a CDN in the traditional way:

Browser support

Axios works in all modern web browsers, and Internet Explorer 8+.

Making Requests

Similar to jQuerys $.ajax function, you can make any kind of HTTP request by passing an options object to Axios:

axios({ method: 'post', url: '/login', data: { user: 'brunos', lastName: 'ilovenodejs' }});

Here, were telling Axios which HTTP method wed like to use (e.g. GET/POST/DELETE etc.) and which URL the request should be made to.

Were also providing some data to be sent along with the request in the form of a simple JavaScript object of key/value pairs. By default, Axios will serialize this as JSON and send it as the request body.

Request Options

There are a whole bunch of additional options you can pass when making a request, but here are the most common ones:

  • baseUrl: if you specify a base URL, itll be prepended to any relative URL you use.
  • headers: an object of key/value pairs to be sent as headers.
  • params: an object of key/value pairs that will be serialized and appended to the URL as a query string.
  • responseType: if youre expecting a response in a format other than JSON, you can set this property to arraybuffer, blob, document, text, or stream.
  • auth: passing an object with username and password fields will use these credentials for HTTP Basic auth on the request.
Convenience methods

Also like jQuery, there are shortcut methods for performing different types of request.

The get, delete, head and options methods all take two arguments: a URL, and an optional config object.

axios.get('/products/5');

The post, put, and patch methods take a data object as their second argument, and an optional config object as the third:

axios.post( '/products', { name: 'Waffle Iron', price: 21.50 }, { options });
Receiving a Response

Once you make a request, Axios returns a promise that will resolve to either a response object or an error object.

axios.get('/product/9') .then(response => console.log(response)) .catch(error => console.log(error));
The response object

When the request is successful, your then() callback will receive a response object with the following properties:

  • data: the payload returned from the server. By default, Axios expects JSON and will parse this back into a JavaScript object for you.
  • status: the HTTP code returned from the server.
  • statusText: the HTTP status message returned by the server.
  • headers: all the headers sent back by the server.
  • config: the original request configuration.
  • request: the actual XMLHttpRequest object (when running in a browser).
The error object

If theres a problem with the request, the promise will be rejected with an error object containing at least some of the following properties:

  • message: the error message text.
  • response: the response object (if received) as described in the previous section.
  • request: the actual XMLHttpRequest object (when running in a browser).
  • config: the original request configuration.
Transforms and Interceptors

Axios provides a couple of neat features inspired by Angulars $http library. Although they appear similar, they have slightly different use cases.

Transforms

Axios allows you to provide functions to transform the outgoing or incoming data, in the form of two configuration options you can set when making a request: transformRequest and transformResponse. Both properties are arrays, allowing you to chain multiple functions that the data will be passed through.

Any functions passed to transformRequest are applied to PUT, POST and PATCH requests. They receive the request data, and the headers object as arguments and must return a modified data object

const options = { transformRequest: [ (data, headers) => { // do something with data return data; } ]}

Functions can be added to transformResponse in the same way, but are called only with the response data, and before the response is passed to any chained then() callbacks.

So what could we use transforms for? One potential use case is dealing with an API that expects data in a particular format say XML or even CSV. You could set up a pair of transforms to convert outgoing and incoming data to and from the format the API requires.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «SitePoint: Modern JavaScript Tools & Skills»

Look at similar books to SitePoint: Modern JavaScript Tools & Skills. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «SitePoint: Modern JavaScript Tools & Skills»

Discussion, reviews of the book SitePoint: Modern JavaScript Tools & Skills and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.