Creating a Simple Server with Express

It is relatively easy to create a test endpoint for web services using Express, perhaps the most popular Node module for web server development. Today, we create a simple shell using a single file. In practice, server applications will be more complex, and this may not be the best strategy for code organization, but it’s a good starting point.

Start out by creating a file called server.js and require a few modules.

var express = require('express');
var bodyParser = require('body-parser');
var util = require('util');

The first of  these modules, express is the basic framework for server development. You will use it whether you are developing traditional web sites or, as is the case here, web services. It is also possible to develop server applications using the core http and https modules, but you should find that using Express makes your code simpler and more modular. Since Express is not a core module, you’ll need to install it

$ npm install --save express

The next module, body-parser simplifies processing of JSON and a few other formats (notably form data). Finally, util provides several utility functions. It’s a core module, so there is no need to install it, but body-parser must be installed in the same manner as express.I

The next step is to create the application object, an instance or express, and set the server port.

var app = express();
app.set('port', process.env.PORT || 3000);

It is somewhat traditional to use port 3000 for node applications. This code actually looks for an environment variable named ‘PORT’ and uses its value as the port on which the application should listen. If it’s not present, the code relies on lazy evaluation of boolean expressions to fall over to 3000. That port value is later used when

//This function starts the server.
function startServer() {
    app.listen(app.get('port'), function () {
        util.log('Sever started on %s mode in port %d ...', app.get('env'), app.get('port'));
    })
};

We recommend using a separate function here to start the server (start listening). That way, the function can be exported or invoked directly. If you want to be able to run your server directly using

$ node server.js

you can use code such as the following

//If file is run directly, start the server. Otherwise, export the application (express) object.
if (require.main === module) {
    startServer();
} else {
    module.exports = startServer;
}

In a real application, the code that actually starts the server may reside in another file, and you’ll want to export startServer(). This code handles both scenarios.w0

At this point, we’re ready to start adding routes. A route is essentially a URL, a  template, or a pattern that the URL should match, coupled with an HTTP method and a function that will be invoked on a request using the specified URL and method. In Express, the function is called a middleware. This may seem like odd terminology, but it is common for requests to pass through several middlewares, all but the last of which is a link in the chain of actions that are performed by the server to handle the given request.

Now, in our example, we are assuming that the request body (if present) will always be JSON, so we add a middleware (which should be at the top) that is handles parsing of request bodies

app.use(bodyParser.json());

Note that we used app.use here. That’s because the middleware bodyParser.json() should be used for all requests with bodies (and not, say, just PUT or POST). We also don’t specify a URL because this middleware should always be used.

Note, by the way, that middlewares are used (where applicable) in the order they appear ion your source code, so oder matters. This may  be counterintuitive to programmers used to functional languages. If you are familiar with the connect module, you will know that the pipelining of middleware is made explicit in connect, but Express doesn’t work this way, and you have to pay attention to the order in which middleware appears in the program source.

Having said that, let’s add another middleware. We want to  return our 404 errors in JSON format, and Express doesn’t do that by default. One easy way to do this is to create a wildcard route and put it at the end

Again, we’re using app.use because this route isn’t method specific, but here we have included an explicit wildcard. This is an example of how pattern matching can be used for the URL.

//Add a wildcard route to ensure that a JSON response is sent in other cases.
app.use('*', function (req, res) {
    res.status(400).json({status: 'error', message: 'request not understood'});
});

We also need a route that will handle error conditions. Place this after the preceding one.

//error route
app.use(function (err, req, res, next) {
    console.error(err.stack);
    res.status(500).json({
        status: 'error',
        message: err.message
    });
});

So far, we haven’t done anything other than set up a basic framework. We haven’t added any code to handle specific requests. This turns out to be surprisingly easy. Suppose we want to respond to

GET /

with a 200 response with the following JSON as its body

{"status":"ok"}

We can do this by adding a new route

//root path
app.get('/', function (req, res) {
    res.status(200).json({status: 'ok'});
});

Notice that this code uses app.get. The reason for this is that the route is specific to the GET method. Notice also that the URL is the literal string ‘/’. It turns out that the explicit response code of 200 is redundant here because Express defaults to 200, but I’ve made it explicit, anyway. The json() method is a convenience that takes a JavaScript object, formats it as JSON  uses it as the response body. (Did you notice that key wasn’t quoted?)

Finally, let’s look at something more complicated

//Update a specific order
app.put('/order/:id', function (req, res) {
    if (!req.body) {
        return res.status(400).json({status: 'error', message: 'no request body'});
    }
    if (req.is('application/json')) {
        res.status(200).json({status: 'ok', message: 'update processed'});
    } else {
        res.status(415).json({status: 'error', message: 'media type not supported'});
    }
});

This route would match a URL such as “/order/10” with the 10 being stored as req.params[‘i] so that we have access to it in the function. We use app.put here because this route will only be used to handle PUT requests. The first thing we do is  check to see if a body is present, and if it’s not, we return a 400 error. Next, we look at at the media type, and return 415 (“Unsupported Media Type”) if it’s not “/application/json”. In real life, we might be using a more specific media type, and the code would have to check for supported media types and handle them as appropriate.

This code just returns a reply indicating success regardless of the request body (so long as it’s JSON). In real life, of course, we would  either perform a database update and  return 200 (“OK”) on success, or perhaps queue it and reply 202 (“Accepted”).

 

 

 

Feeling Connected

    As I write this, I am in the Rocky Mountain National Park, near Estes, Colorado. Even though it’s now after 9 p.m. the trees (I think spruce and pine, but I’m not sure) and snow are simply amazing. I’ve never been here before and had no idea how many elk I would see close up. They are really amazing creatures. Of course, there is other wildlife (rabbits, wild turkeys, and a fox on the prowl) but we always seem to be drawn to the big animals. Why is that?

The Rocky Mountsins really are imposing. Traveling to Denver from Mesa Verde, and ultimately from Utah and California, meant crossing the continental divide at Monarch Crest (something like 11,231 feet (I obviously can’t go online to check from here).  But numbers really aren’t the point, are they? I can’t help but wonder what the first people to travel here from the eastern United States must have thought. They might have known the geologically much older Appalachian range (which have weathered over the years, becoming much smaller and rounded), or perhaps the Catskills in New York. There is just no way that prior experience could have prepared them for what we have our here. Even the relatively smaller coastal ranges such as the Sierra Nevada don’t really compare I’m shear size. And let’s not forget that the Sierra are home to Yosemite National Park, and also where the Donner party met it’s unfortunate fate. The Sierra is also a geologically young range and, by the way, the word Sierra means “jagged”. 

Okay, it’s darker now, with only a  band of light visible above the mountains (reflected from the clouds) and stars are now visible. I wonder what the sky would look like if the moon weren’t full tonight.  It rained for quite a while this evening and the skies are just beginning to clear. For someone who calls California home, this rain is actually rather refreshing (and by the way, there is drought throughout the west, it’s just at its worst in California.) I had wondered if it would snow before morning, but that seems unlikely. 

What is the appeal of nature? Beauty is one answer, and perhaps one of the easiest. I don’t know that there’s any one answer, but I think it helps us to feel connected, both with the world around us and, in a way, with ourselves.  I think we do a lot of things, such as studying history and genealogy, or even mathematics and physics, because it helps us to feel connected – to our ancestors and what they must have experienced, to the natural world, to our families and society as a whole. 

You’re probably wondering what I’m doing tapping this out on an iPhone when I’m out here. To tell you the truth, so am I. 

Tales Along the Way

[This is another excerpt from pioneer biography. In this case, an account by Erastus Snow Carpenter of an incident during the journey by wagon train undertaken by his parents (John Steel Carpenter and Margarett McCullough Carpenter) in 1857.]

John Steel CarpenterMother had the desire to gather to Utah, then the gathering place of the Saints. Through the kindness of an uncle, Joseph Crossgrove, husband of father’s sister Rachel, she was permitted to gratify her wish. Nothing particular or out of the common transpired up to the time we prepared to go to Utah. In the spring, I think April, 1857, we took took steamer at Wilmington, Delaware, for Philadelphia. From there, we took the train to Iowa City. Near there our outfit was assembled to cross the great plains, some 1300 miles to Utah. Joseph had three wagons, with two yoke of oxen to each.. One of them was for the accommodation of Mother and her family. Ours was an independent train, that is, the individuals who composed it owned their own wagons and teams. A returning missionary, Jacob Huffiness was selected as captain, as he had been over the route and knew more about the country than any of our company did. There were two or three handcart companies being got up at that time.

We traveled along for some time, one ahead and then the other. Everything went well until we got pretty well up the Platte River. We had lost an ox now and then, but nothing serious. After we got some distance up the Platte, our oxen became uneasy and would stampede frequently. Thinking to make them more secure, the wagons were made into a corral, as was the custom of all trains crossing the plains. The cattle were then driven inside. During the night they made a rush to get away and tipped over one wagon, hurting one or two persons who were sleeping under the wagon. Shortly after this, we had another stampede, when some seventy of the oxen got away. Some of the men followed them for three or four days, but couldn’t overtake them. They found three head on the trail, but they were so nearly given out that they didn’t amount to much. This was a great loss to the company, and although their loads were somewhat lighter than when they started, they had to hitch up everything they had in the company. There were several cows that were put into the yoke and made to do service; One young man in the company, who had white cow in his  team, said the cow was the best ox he had.

Soon after this, the teams stampeded with the wagons toward the river which was half or three-quarters of a mile away. Before reaching there, they all stopped suddenly of their own accord, and stood perfectly quiet. There were three or four wagons jammed in side by side so close that they couldn’t pull them apart with the teams. Men had to lift term apart in order to move them. Not a thing was broken.

Source: Erastus Snow Carpenter Family Association, Erastus Snow Carpenter (Provo:  Community Press, 1985), 3

Cleon Earl Jackson Would be 100 Today

My grandfather Cleon Earl Jackson was born to Robert Earl Jackson and Sarah (Judd) Jackson April 22, 1915 in Fredonia, Cleon Earl JacksonArizona. I write this from the nearby town of Hurricane, Utah. Cleon Jackson would marry Mildred Carpenter in 1939 in Kanab, Utah and eventually settle in the nearby town of Glendale, Utah. He worked at a number of trades but I always knew him as a heavy equipment (Caterpillar) operator. I don’t know  when he became a Caterpillar operator (or “cat skinner”, as it is sometimes called), but his enlistment papers in 1945 list heavy equipment operator as his occupation. You can see his road cuts all over southern Utah. I don’t quite have the eye, but my mother and her family can spot them instantly. Of course, his work was not limited to highway construction. According to family tradition (and someday I will have to document this) he pulled the first cable over Glen Canyon Dam.

He spent a lot of time outdoors and had an amazing eye. I still remember a time when my sister and I were in a pickup truck with him (heading for an area known as “the bench”) when he turned the truck around and stopped to show us a tarantula crossing the road. He had many skills, not the least of which was that he spoke fluent Navajo. That is one of the many things I would have loved to have heard more about, but it’s not something he talked about much, at least with us. One thing I always admired about him is that he would hunt to feed his family. I don’t remember him as being a typical sportsman, but someone who knew how to live off the land. He loved to read. I’m told he’d read the Old Testament for pleasure and laugh at many of the stories (which, let’s be honest can be funny at times). To me, that’s a great example of how to take scripture seriously, both as literature and a religious text. There is so much that we could learn from that example.

Unfortunately, he died fairly young, February 7, 1981. I miss him.

A Mystery Solved – Augustus Edwin Austin

 

 Augustus Edwin Austin was my great grandfather. I know from his death certificate that he died from a cerebral hemorrhage (brain bleed) that resulted from a cranial fracture. i knew that he died at LDS Hospital in Salt Lake City. My understanding, based on various comments, was that the injury was sustained elsewhere, possibly in Idaho, but I could not prove this. I had no idea why he wouldn’t have been hospitalized before returning to Utah. I also had no idea what the nature of the accident (I assumed it was an accident) was that could lead to such an injury. 

I tried Google, looking for Augustus Edwin Austin and accident, but to avail. When that didn’t work, I tried turning to newspaper archives but didn’t make much more progress. I found death notices and the cause of death, but no details. In retrospect, one mistake I made was searching for information about an accident. He wasn’t injured in an accident, it was assault. 

I learned this through another archive (this time using MyHeritage.com. Here’s what I found:

Ed Austin Dies, Third Victim in a Row over a Robe 
Kalispell -Oct. 6 (AP) – Edward Augustus Austin, who died at Salt Lake City yesterday, was struck over the head at Kila, Aug. 17 by Dan “Dody” Duncan after the latter had shot and killed Bud Neas and Russell Austin. 

Edward Austin was in a hospital here until September 11, when he was discharged. His physician said that he lost the  sight in one eye and that a fractured skull received in the beating could have produced the death at Salt Lake City.
The fatal shooting and beating climaxed a quarrel over the ownership of a cowhide robe.

Authorities said Duncan was pummeled by Russell Austin and retaliated by shooting him and Neas and beating Edward Austin with the weapon.

Now that is some story! I still don’t know who any of these other people were, even Russell Austin. I’m not sure what the “row” was. That’s a word that almost makes it sound like a bar fight. I’d expect a mugging, but it sounds like there was quite a fight, involving several people. It could have been almost anywhere: in a home, a business, camp. Anywhere. We may never know the details, but this certainly adds character Edwin Austin (and yes, that is his correct name). 

About John Woodhouse

John Woodhouse

John Woodhouse

John Woodhouse was born in Adwick Le Street, Yorkshire, England (near Doncaster). He was taught the trade of tailoring by his father, though he was the first of his family to join the Church of Jesus Christ of Latter-day Saints (Mormon church) in 1848. The rest of his family followed him, and they emigrated to the states on the ship “Ellen” in 1851. It departed Liverpool January 8, 1851, arriving in New Orleans March 14. This was one of the last several LDS Pioneer ships to sail to New Orleans, perhaps due to high morality due to disease. Later ships (from Europe) typically sailed to New York.

They first settled in St. Louis Missouri where his father died, apparently while working in Illinois. His brother Norman also died in St. Louis, but the rest of the family would travel west in 1852 with the Jepson company at age 21 (departing May 29, 1852 and arriving in September 10.)

John Woodhouse lived to be 86, when he died September 10, 1916 in Lehi, Utah after being struck by a train during a late night walk.

Sources

Ancestry.com or MyHeritage.com? It’s not clear. 

Should you choose Ancestry.com or MyHeritage.com for online genealogy research? Of these two services, Ancestry.com advertises the most aggressively, is the better known of the two, and has an edge in the attractiveness and usability of the online tools it offers. Ancestry also claims to offer the the greatest number of records online, and from what I can tell, this appears to be true. Both Ancestry and MyHeritage offer the same basic resources for vital records such as birth and marriage records, census data and so forth, but there are types of records that are not yet available through MyHeritage. All in all, they both offer impressive repositories, and to cut to the chase, I think they are both perfectly viable options. I have used, and will probably contine to make use of, both of them. 

Of course, there are disadvantages to Ancestry.com, too. The desktop tool, Family Tree Maker, is not free, though it is reasonably priced. By contrast, MyHeritage.com offere a free tool called Family Tree Builder. To me (a Mac user), it feels a little clunky, particularly if you run it under OS X using Wine. But it offers validation tools not available, at least as a report, in Family Tree Maker. It also gives you the opportunity to export data in the form of spreadsheets. With Family Tree Maker, your only option is GEDCOM 5.5, and your data is otherwise pretty much locked up. On the plus side, MyHeritage.com offers superior integration with FamilySearch.org, the family history site operated by the LDS church and with GENi World Tree. With MyHeritage, you can extract data from both of these services with suitable source citations. With Ancestry.com, you do get matches against FamilySearch.org as historical records (in their terminology, which is a bit misleading in my opinion). 

Another area where Ancestry.com has an advantage is in the handling of multimedia. First, it makes it easier to discover photos and images and to include them in your database. You can do this with MyHeritage.com, too, but the process is not as well integrated with the software. Another really nice feature of Ancestry.com is that it saves digital images of your source documents when available. I find this very useful, and have often opened these files, either to validated indexed data or because I was looking for additional information. Having ready access to digital images is a huge boon, though it can really add to the size of your database. 

That brings us to the topic of bugs and design problems. Both Ancestry.com and MyHeritage.com have iOS apps, and of the two, I like the Ancestry.com app much better. It offers many more features and makes research a lot easier. You can search using the MyHeritage.com app using their Super Search pane, and you can edit records, but that’s pretty much it. One unfortunate thing about the Ancestry app is that it doesn’t add proper source citations. It does include a hyperlink so that you can go into Family Tree Maker later and add the source citation later, but I can’t understand why the app doesn’t do this. I consider this a bug, and hope that it will be addressed in a future version. 

So, what is the conclusion here? I don’t think there is a clear winner, and I actually use both, though it is certainly an inconvenience to do so. Beyond that, neither service is free. They do offer free accounts, but there are either limits on the records you have access to, the size of your database, or both. If you’re LDS and doing family history research for religious reasons, you may find MyHeritage.com a bit easier, and may wish to look at Roots Magic, but beyond that, I can’t make a clear recommendation for one rather than the other. 

The Death of Charles Woodhouse

Charles Woodhouse's ViolinCharles Woodhouse, my third great grandfather was a tailor living in Adwick Le Street, Yorkshire, England. His son, John Woodhouse, who is my second great grandfather, was converted to the LDS (or Mormon) church when he was nineteen. His entire family joined, and they would eventually travel from Liverpool to New Orleans on the emigrant ship Ellen, followed by  seven day journey by riverboat to St. Louis, Missouri. John Woodhouse and most of the family would travel by covered wagon (in the Jepson Company) to Salt Lake City. But Charles died in a drowning accident, recorded, almost in passing, by John Woodhouse in his pioneer journal (on p. 20):
During our stay in St. Louis my brother Charles had a severe sickness his living through it was a marvel. We lost our youngest brother Norman, and my father was accidentally drowned over in Illinois where he was at work.
Unfortunately, John Woodhouse’s Pioneer Journal is not a contemporary account, but a series of recollections written down quite a bit later, in Utah. He did keep a journal, but the it was lost, presumably during the journey. It seems that he did not want to dwell on the details of his father’s death.

There is a family tradition that he was actually in Illinois where he was performing with a group of musicians where he became drunk, fell into the river, and drowned. There are actually several versions of the story, and it has described as a “hole”, a flooded basement, and even a barrel! One version of the story can be found at findagrave.com (memorial #40176198):

Charles Woodhouse died in St. Louis, Missouri. He was coming home from performing on his violin with a musical group to earn money to travel on to Utah. He had too much to drink and fell in a open hole filled with water. He was found floating with his violin floating next to him.

[Read more…]

Will of Aaron Jackson

Today, we have another in a series of historical documents. Aaron Jackson (1783-1837) was a Pennsylvania farmer and father of Jesse Taylor Jackson. I haven’t been able to learn much about him, but this document tells us that he owned a farm (he calls it a plantation) of about 70 acres, and it seems a fair amount of livestock (horses and cattle), not to mention hogs. He bequeaths it all to his wife Abigail during her life, and then what remains to his children after her death. In addition to confirming where he lived and his property holdings, this document provides and independent source for dates and the names of his children (all of whom we know of through other sources). I have corrected OCR errors but retained the spelling of the original document. [Read more…]

Using GeoNames for Genealogical Research

EuropeWe’ve talked about the importance of normalizing place names in your family tree. You may find the same state name written “Massachusetts”, “Massachusetts, USA”,”Mass.”,  or just  “MA”, and that’s just the beginning. Within states there are counties, boroughs, cities, towns, and local geographic references. The same places will frequently be referred to differently in different records, and it is important to know when two records refer to the same geographic location. There are several reasons for this, the most obvious being that you need to know whether the records refer to the same person. As an example, I have a grand aunt who seemed, according to some records, to have died in Florida. I was pretty sure this was not the case, but thought there was a small possibility that she could have moved there late in life, and I didn’t know. It turns out that another woman with the same first and last name (married name) who was born on the very same day, did die and was buried in Florida. But my grand aunt was buried in Utah, just as I suspected. In this case, the place names were so different that Citrus, Florida stuck out like a sore thumb, and I didn’t miss it. But it could have been different. What if my aunt were buried in a different part of Florida, possibly somewhere with a name I didn’t know? That’s the kind of discrepancy that it would be easy to miss, and I very well could have added quite a bit more inaccurate data to my family tree before the error was discovered.

Software can be very helpful in working with place names, but it can also make us vulnerable to other errors. As a simple example, I once encountered references to a colonial ancestor living in Germany. How could that be? In case you haven’t already guessed, she lived in Delaware, and someone recorded it as DE, the standard U.S. state abbreviation for Delaware. But DE is also the International 2-letter code for Germany (Deutschland in German). International standards (such as ISO 3166 for country codes) are indispensable in representing place names unambiguously and, if you’re like me, you use state abbreviations without giving it much thought. Unfortunately, computer applications often simply digitize paper forms, and since people have been writing addresses on one or a few lines for ages, computer programs tend to provide so-called free text fields for place names. And that’s where the trouble starts. In order to compare place names software needs to parse these fields into their constituent components.This is often done heuristically, so “Dover, DE” will be correctly interpreted as Dover, Delaware, and “Hamburg, DE” will correctly be interpreted as Hamburg, Germany. But in this case, something went wrong. Most likely, the software was unable to identify the name of the town or settlement after consulting a geographic database, so it fell back on the interpretation of DE as referring to Germany. The moral of the story is that you should always manually review place names before storing them in your family tree or database.

Of course, there is a problem, there are a lot of place names, and no matter how extensive our geographic knowledge, we are likely to encounter names we don’t recognize. Worse, we may think a place name is correct or complete when, in fact, it is not. This is where tool support comes in. Popular genealogy applications such as Family Tree Maker include geographic databases and include tools allow you to validate and correct place names. But what if you don’t use one of these tools? GeoNames is an open source database (licensed under the Creative Commons 3.0 Attribution license) that includes over eight million records, and it is freely available on the web. You can either use the web based interface, download the database or make use of the web service interface. Most of the time, you’ll probably want to use the form on the web site to look up place names using your browser. The other options are primarily of interest to application developers.

So, how does it work? Let’s suppose that the place name Adwick Le Street, Yorkshire, England is unfamiliar to us, or we are unsure it is spelled correctly. Head over to GeoNames at http://www.geonames.org and type “Adwick Le Street” in the search box, and select “United Kingdom” from the drop down box to the right of it. Press Search. You will see something like this

2 records found for “Adwick Le Street”
Name Country Feature class Latitude Longitude
1 P Adwick le Street  wikipedia article
United Kingdom, England
Doncaster > Brodsworth
populated place N 53° 34′ 14” W 1° 11′ 4”
2 S Adwick le Street Castle Hills
United Kingdom, England
Doncaster
castle N 53° 33′ 14” W 1° 10′ 9”

In this case, there is no need to use the advanced search option. If you like, you can click on the hyperlink to see Adwick Le Street on a map. This can help to resolve apparent ambiguities. For example, in the case of my ancestor John Woodhouse, I had seen him described as living in Doncaster as well as Adwick Le Street. As it happens, Doncaster is the nearest town. That tells me that I’m not looking at two place names (well, distant ones, anyway), and I don’t have to worry about having made a mistake. But if one source said he was born in London and another in Doncaster, I would have a problem, and would need to do further research to resolve the ambiguity.