Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Created March 6, 2015 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/4e2cb8cd56af2194be0c to your computer and use it in GitHub Desktop.
Save thomaswilburn/4e2cb8cd56af2194be0c to your computer and use it in GitHub Desktop.
Instructions for AJAX testing

Local Hosting

Getting started

There are lots of ways to run a local host, which is necessary for testing AJAX (and useful when it comes to other tasks). This document covers setting up a simple Node-based HTTP server, but you may also choose to use one of the MAMP/WAMP applications to start a local Apache instance, or use Python's simple server.

Before doing anything else, install the latest version of NodeJS. Node is a runtime for desktop/server JavaScript (as opposed to running scripts in the browser), and many useful tools are built on top of it. If you find this helpful, you may also want to look into LESS, Grunt, and other Node-based development tools.

Starting a command line

We'll be starting our server from the command line, or "shell." Although it seems primitive, the command line is an extremely useful way to run commands and automate various development tasks. The easiest way to launch a shell is through the GitHub software. On Windows, make sure that your GitHub options have "Git Bash" selected as the choice. Then, on either platform, right-click your repository name in the list on the left and select "Open in Shell" or "Open in Terminal." This will start a command line window that's already located in your repo directory.

You can also start a shell session and navigate to a different directory. On OS X, start the Terminal program that comes built-in. On Windows, use the Git Shell or Git Bash that should have come installed with GitHub. Once the shell opens, use the following commands to navigate to the folder where you want to start your server:

  • cd - change directory (ex: "cd Desktop" will enter the Desktop folder if one exists in the current directory. "cd .." will go up to the parent folder.)
  • ls - list current files and directories
  • pwd - show the current directory path (Print Working Directory)

Installing http-server

Once we have a shell open, we're going to use it to install the http-server command. We'll install it globally, meaning that we can use it anywhere on your computer. Use the following command:

npm install http-server -g

NPM is the Node package manager, and is installed along with Node. On OS X, you may need to add "sudo" to the front of the command, and then enter your password, since global commands are considered an "admin" task.

You'll see the NPM task spin for a little while, followed by a readout of the installation. If there are any errors, please feel free to let me know, and I can help you solve them.

Starting the server

Creating the local dev server at this point is easy: from the directory that you want to treat as your server, run the http-server command and specify a port (we've been using 8000):

http-server -p 8000

If you don't specify a port with the -p flag, it will pick one for you. Once the server is running, you can open a web browser to localhost:8000 (or whatever port) to connect to the folder. I would recommend naming your file "index.html" so that it'll be served automatically, otherwise you'll have to figure out the path.

When you're done working with the server, press Ctrl-C to cancel the server process (it will remind you of this). Once the server is stopped, you can close the command line safely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment