Posts

Showing posts with the label Nodejs

Fetch

fetch() allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API, avoiding callback hell and having to remember the complex API of XMLHttpRequest. The Fetch API has been available in the Service Worker global scope since Chrome 40, but it'll be enabled in the window scope in Chrome 42. There is also a rather fetching polyfill by GitHub that you can use today. If you've never used Promises before, check out Introduction to JavaScript Promises. Basic Fetch Request Let's start by comparing a simple example implemented with an XMLHttpRequest and then with fetch . We just want to request a URL, get a response and parse it as JSON. XMLHttpRequest An XMLHttpRequest would need two listeners to be set to handle the success and error cases and a call to open() and send() . Example from MDN docs . function reqListener () {     var data = JSON . parse ( th...

Nodejs

Node.js is one of the biggest explosions in the past few years. Having the ability to run JavaScript (the client-side language many are familiar) on the server is an enticing notion. Front-end developers that know JavaScript well can easily expand their knowledge to know back- end server-side code. Node is built on Google Chrome’s V8 JavaScript runtime and sits as the server-side platform in your MEAN (https://en.wikipedia.org/wiki/MEAN_(software_bundle)) application. So what does that mean? In a LAMP stack you have your web server (Apache, Nginx, etc.) running with the server-side scripting language (PHP, Perl, Python) to create a dynamic website. The server-side code is used to create the application environment by extracting data from the database (MYSQL) and is then interpreted by the web server to produce the web page. When a new connection is requested, Apache creates a new thread or process to handle that request, which makes it multithreaded. Often you will have a number of idl...