We’ve been using “connect” as the basis for our middleware so far. In many cases, you’ll want to be able to set up handling for specific routes using specific REST verbs, as well as read request parameters and easily pipe non-html data to the client. “express” is a package that extends “connect”, and is essentially the “gold-standard” from which to build HTTP applications in Node.js
Install express now:
We will use express in place of connect for the remainder of this tutorial.
As I have mentioned before, npm has a huge number of packages, and finding something that works the way you want can be a little bit daunting.
We’re working on a simple UI that can take a query from the user, and locate entries from Wikipedia that make sense.
I have located a package that can take care of the actual query to wikipedia ‘wikijs’, so let’s install that in our project now.
Next, we want to provide a basic GET endpoint to our connect server that accepts a query, and returns some JSON answers. Update your “server.js” file as folllows:
Go ahead and fire up “supervisor server.js” and navigate to http://localhost:3000/search?q=node
If everything is working, this should produce an array of json results, now all we have to do is wire up the interface to take advantage of this new method.
Now, navigate to http://localhost:3000, if everything went well, you should now be able to type a query and get links to wikipedia articles.
Obviously, this is a pretty silly, because wikipedia actually provides this functionality, but it hopefully shows how to create a basic node web-application, from end to end.
In future posts, I’ll go into some of the other aspects of building production-ready node.js applications, and some other “gotchas” that I’ve been learning about along the way.