Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ECMAScript Cookbook

You're reading from   ECMAScript Cookbook Over 70 recipes to help you learn the new ECMAScript (ES6/ES8) features and solve common JavaScript problems

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788628174
Length 348 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ross Harrison Ross Harrison
Author Profile Icon Ross Harrison
Ross Harrison
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Building with Modules 2. Staying Compatible with Legacy Browsers FREE CHAPTER 3. Working with Promises 4. Working with async/await and Functions 5. Web Workers, Shared Memory, and Atomics 6. Plain Objects 7. Creating Classes 8. Inheritance and Composition 9. Larger Structures with Design Patterns 10. Working with Arrays 11. Working with Maps and Symbols 12. Working with Sets 13. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image