Chapter 10. Advanced server methods
This chapter covers
- Understanding the difference between synchronous and asynchronous functions
- Using asynchronous functions
- Integrating external APIs
- Uploading files
Even though Meteor is an isomorphic platform, some things can only be done in certain environments. This chapter introduces you to some advanced concepts on the server. With Node.js running in the background, it’s time to take a closer look at the event loop and the right way to work with asynchronous code. This is especially useful if you plan to have your application communicate with external APIs.
While we’re looking into server specifics, we’ll also discuss a simple way to upload files to the server. The code in this chapter should run on the server unless stated otherwise.
The foundation of the Meteor stack is Node.js (see figure 10.1). Technically, it’s a server implementation of the V8 JavaScript engine that can also be found in the Google Chrome browser. Therefore, it comes as no surprise that Node.js shows the same characteristics as a browser. It’s because of two reasons:
- Node.js is event driven.
- Node.js uses nonblocking I/O.
These two characteristics make Node.js stand out from other server technology such as PHP or Rails that typically execute code in a linear or synchronous way. But even if Node.js is by nature nonblocking, you can still write blocking code.