CouchDB and PHP Web Development Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action — checking that CouchDB is running

We are going to attempt to hit the CouchDB service on our machine by using the command line utility cURL (we'll spell it curl from here on out for the sake of simplicity) that allows you to make raw HTTP requests. curl is our main method of communication with CouchDB. Let's start with a curl statement to talk to CouchDB.

  1. Open Terminal.
  2. Run the following statement to create a request that will hit CouchDB:
    curl http://127.0.0.1:5984/ 
    
  3. Terminal will reply with the following:
    {"couchdb":"Welcome","version":"1.0.2"} 
    

What just happened?

We just used curl to communicate with the CouchDB service by issuing a GET request. By default, curl will assume that we're trying to issue a GET request unless we tell it otherwise. We issued our curl statement to http://127.0.0.1:5984. Breaking this resource down into pieces, http://127.0.0.1 is the IP of our local computer, and 5984 is the port that CouchDB runs on by default.

Running CouchDB as a background process

If we were to stop our configuration here, you would have to run couchdb b each time you started development. This will quickly turn into a pain point for us. So, let's run CouchDB as a system daemon that will always run in the background, even after you restart your computer. In order to do this, we will use a Mac OS X service management framework, called launchd, that manages system daemons and allows them to be started and stopped.

  1. Open Terminal.
  2. Kill the background process of CouchDB, by running the following command:
    couchdb -k 
    
  3. If CouchDB was running, it would return the following text:
    Apache CouchDB has been killed. 
    
  4. Let's run CouchDB as a real background process and ensure that it starts each time you start your computer by running the following statement:
    launchctl load -w /usr/local/Cellar/couchdb/1.0.2/Library/LaunchDaemons/org.apache.couchdb.plist 
    

    Note

    If your version of CouchDB is different to mine, you will have to change the version in this script that says "1.0.2", to match your version.

CouchDB is now running in the background, and even if we restart our computer, we don't have to worry about starting the service before we try to use it.

If, for some reason, you decide that you don't want CouchDB running in the background, you can unload it by running the following command:

launchctl unload /usr/local/Cellar/couchdb/1.0.2/Library/LaunchDaemons/org.apache.couchdb.plist 

You can double-check to make sure that CouchDB is running by using the curl statement that we used earlier:

  1. Open Terminal.
  2. Run the following command:
    curl http://127.0.0.1:5984/ 
    
  3. Terminal will reply with the following:
    {"couchdb":"Welcome","version":"1.0.2"}