Book Image

ECMAScript Cookbook

By : Ross Harrison
Book Image

ECMAScript Cookbook

By: Ross Harrison

Overview of this book

ECMAScript Cookbook follows a modular approach with independent recipes covering different feature sets and specifications of ECMAScript to help you become an efficient programmer. This book starts off with organizing your JavaScript applications as well as delivering those applications to modem and legacy systems. You will get acquainted with features of ECMAScript 8 such as async, SharedArrayBuffers, and Atomic operations that enhance asynchronous and parallel operations. In addition to this, this book will introduce you to SharedArrayBuffers, which allow web workers to share data directly, and Atomic operations, which help coordinate behavior across the threads. You will also work with OOP and Collections, followed by new functions and methods on the built-in Object and Array types that make common operations more manageable and less error-prone. You will then see how to easily build more sophisticated and expressive program structures with classes and inheritance. In the end, we will cover Sets, Maps, and Symbols, which are the new types introduced in ECMAScript 6 to add new behaviors and allow you to create simple and powerful modules. By the end of the book, you will be able to produce more efficient, expressive, and simpler programs using the new features of ECMAScript. ?
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
PacktPub.com
Contributors
Preface
Index

Installing Python, using SimpleHTTPServer to host a local static file server


It is possible to browse web pages directly from the filesystem. However, Chrome and Firefox have security features that make this inconvenient for development. What we need is a simple static file server. This recipe demonstrates how to install Python (if necessary) and use it to serve files from a directory.

Getting ready

Find out how to open the command line on your OS. On macOS and Linux, this is called the Terminal. On Windows, it is called the Command Prompt.

You should use a browser that is configured to load ES modules (see the first recipe).

How to do it...

  1. Check whether you have Python installed already.
  2. Open the command line.
  3. Enter the following command:
python --version
  1. If you see an output like the one displayed as follows, Python is already installed. And you can skip to step 6:
Python 2.7.10
  1. If you receive an error such as the following, continue with the installation in step 5:
command not found: python
  1. Install Python on your computer:
  2. Create a folder on your desktop named es8-cookbook-workspace.
  3. Inside the folder, create a text file named hello.txt and save some text to it.
  4. Open the Command Prompt and navigate to the folder:
  5.  In the Linux or macOS Terminal enter:
cd ~/Desktop/es8-cookbook-workspace
  1. On Windows type the following command:
cd C:Desktopes8-cookbook-workspace
  1. Start the Python HTTP server with the following command:
python -m SimpleHTTPServer # python 2

Or we can use following command:

python -m http.server # python 3
  1. Open your browser and enter the following URL:http://localhost:8000/.
  1. You should see a page that shows the contents of the es8-cookbook-workspace folder:
  1. Click on the link to hello.txt and you'll see the text contents of the file you created.

How it works...

The first thing we did was check if Python was installed. The best way to do this is to ask Python for its version number. This way we know whether Python is installed, and if it's new enough for our purposes.

If it's not installed, Python can be retrieved via the OS's package manager, or via the installers made available through Python's website.

Once installed, Python comes with a lot of utilities. The one we are interested in is the appropriately named SimpleHTTPServer. This utility listens for HTTP requests on port 8000, and returns the contents of the files relative to the directory root. If the path points to a directory, it returns an HTML page that lists the directory contents.