BT

Your opinion matters! Please fill in the InfoQ Survey!

Q&A on Express.js with Evan Hahn

| Posted by David Iffland Follow 3 Followers on May 05, 2016. Estimated reading time: 9 minutes |

When people talk about Node.js powering the back-end web, they're often actually talking about Express. Express.js is a thin, unopinionated layer built on top of Node.js and works to eliminate much of the heavy lifting often associated with creating a node.js web server. Just as jQuery and other frameworks smooth over the XmlHttpRequest work in the browser, Express.js works to make the experience on the server just as easy.

Evan Hahn has written an excellent book, "Express in Action" that covers how to get started web applications with Express. The book is divided into three sections. For those who haven't heard about Express and are looking to get a feel for how it works, the first section provides an intro along with the basics. I consider this section a more useful primer than the actual Express website.

For those ready to get started, section two drills into the most important pieces of Express. In building my own Express applications, I knew I was using the middleware system, but it wasn't clear exactly how that middleware was working; flowing a request from piece to piece. Evan plainly and humorously lays out how the system works.

Unless you're writing a single purpose web app, you're going to need to route requests to different handlers. Evan goes over how to set up routing and sub-applications in a way that makes it more obvious how the pieces fit together. For those building web sites, Evan shows how to the view middleware works with two popular options, Pug and EJS.

In the final section, entitled "Express in Context", Evan walks us through hooking up Express to other back-end systems to build a more complete (and more useful) service. He uses the Passport middleware to provide user authentication and MongoDb to build out a database back-end. He shows how to use the Mocha testing framework in concert with the Chai assertion library to craft a suite of automated test. He even provides a brief introduction to LESS, how to use Browserify and Grunt, and how to deploy to end result to Heroku. It's a broad and valuable section that condenses hours of internet searches down to a few chapters.

InfoQ had the opportunity to speak with Evan about his book and where Express is going.

InfoQ: Why did you want to write a book about Express.js?

Evan Hahn: In short, I wrote it because I wanted a book that explained Express at a conceptual level.

I was trying to learn Express. I found a lot of good tutorials that showed you how to use the framework, but nothing that gave me a deeper understanding of how it worked. I proclaimed myself an expert and wrote the blog post I wanted.

This blog post became pretty popular on my website! I was amazed when it appeared at the top of JavaScript Weekly, which is my favorite email newsletter.

Manning Publications reached out to me and asked if I'd like to expand the blog post into a full book about Express. I said yes. The goal was to make the book version of the tutorial—something that gave a deeper understanding.

InfoQ: In the book, you mentioned that Chapter 3 (Foundations of Express) was your favorite. Can you tell us more about what makes that chapter so important?

Hahn: There are lots of times that I'm just trying to get something working, and if it works, I don't care how. I feel this way about my microwave.

But there are other things (mostly code-related) that I want to understand, both to be better equipped and to satisfy my curiosity. I don't just want to follow a recipe when I cook; I want to understand the ingredients and what they mean to the meal.

Chapter 3 aims to give people that conceptual understanding. If it works as intended, it'll help readers contextualize the rest of the book (and any other Express information they see).

InfoQ: Middleware is such an integral part of Express, but it feels a bit nebulous. Can you expand more on how Express uses middleware?

Hahn: I think middleware is Express's biggest conceptual hurdle. I didn't understand it right away.

If you're not using any libraries, Node's HTTP server basically wraps a single function you write. All of your behavior has to live in that function. You can call out to other functions, but it's one big function doing everything.

This creates organization problems for even small-sized applications. Having one thing in one giant function gets tricky!

Express apps, at their core, are just an array of smaller functions, each one handling a small piece. These smaller functions are called "middleware" (which I don't think is a great name, by the way). When a request comes into an Express app, it starts at the first middleware. The first middleware does its thing and then it continues to the next middleware if it chooses, or it can stop. The next middleware does its thing and continues to the next middleware if it chooses.

"Middleware" isn't something Express invented; it's a concept that exists in a lot of other frameworks in a lot of other languages.

InfoQ: Express is an unopinionated platform; a blank slate. Yet, that can cause some confusion and stress to newcomers. What would an opinionated version of Express look like?

Hahn: I think the best example is Sails.js, a framework built on top of Express that has a lot more opinions. It adds file organization and database integration and more.

InfoQ: How prevalent is Express? Is it suitable to use for a high performance website?

Hahn: I spend a lot of time reading about JavaScript so my personal experience colors my impression of the zeitgeist. But we can look at some data:

The Node Foundation recently found that "Express...is used by an average of 83 percent of Node.js developers," so I think it's fair to say that it's popular within the Node community. But what about Node in general?

StackOverflow's 2016 developer survey found JavaScript to be the most popular programming language and Node to be in the top tech topics.

I've seen other data that show faster-growing or more popular tech (like in the TIOBE index), but I've seen no data that suggest that Node or Express are on their way out.

Express is not the fastest web framework out there, but it's pretty quick. Express piggybacks off of Node's evented I/O model. I feel like this is one of the most important performance tricks a web app can have up its sleeve. From my experience, the slowest parts of your web app aren't in raw CPU—they're in interactions with the HTTP connections or the database or the file system. Express and Node don't (typically) block waiting for those interactions to finish, which is really nice.

Companies like Uber and LinkedIn use Express, if that makes you feel any better!

InfoQ: Will we ever see HTTP/2 for Express?

Hahn: There's some discussion ("Support for module http2?", "Feature request: friendly exposure of req/res logic for HTTP/2 push promises") about that and it's on the Express 5.0 roadmap—that's all I really know!

InfoQ: There are some who think that we should just all move on from Express and start using something like Koa or Hapi. In the fast-paced world of JavaScript, is the sun setting on Express?

Hahn: I think the hype around Express and Node is fading, but that doesn't worry me—I feel like technology finds its proper footing after its 15 minutes of fame.

Ruby on Rails had its time in the spotlight, and while it's hardly considered a "new hotness", it's still a great way to build a certain kind of web app. People don't seem to be choosing Rails because "well, it's Rails!" anymore - they pick it for considered reasons.

I think the same is happening to Express. People will stop using it because it's the hip thing to do—people will use it because it's a good tool for building a certain kind of web application.

By the way, I think Koa and Hapi are both very cool frameworks! If you're building a Node app, they're definitely worth considering along with Express.

InfoQ: A few months ago, there was some controversy surrounding Express. Can you explain what the current state of Express is?

Hahn: There's a fantastic blog post called "The Unbelievable History of the Express JavaScript Framework" that describes this better than I can, but I'll do my best:

The lead maintainer, Doug Wilson, of Express recently said, "I am closing down Express 5.0. I'm quitting the Express TC now. I cannot trust anyone any more." This was in response to Express's supposed corporate owners (IBM by way of subsidiary StrongLoop) treating Mr. Wilson poorly. He did a tremendous amount of work and claimed that he wasn't getting the communication or support he needed.

The GitHub repository was transferred to an organization called "expressjs".

Express 5.0.0 has been in alpha since November of 2014 and 5.0.0-alpha.2 was released 8 months later (to the day); that's the latest news on the 5.0.0 prerelease. Its latest 4.x release was on February 26, 2016. Its future is a bit uncertain!

InfoQ: What advice do you have for developers that are just starting to take a look at Express?

Hahn: I wrote a tutorial that explains the framework at a conceptual level, which might help beginners.

I think you should make sure you're starting with a solid foundation. Make sure you know how JavaScript callbacks work (excellent video); make sure you can run a simple "Hello World" program in Node; make sure you have a basic understanding of what npm is (hint: it's a package manager with a few other helpful features).

Once you've got a solid footing, I think you can go one of two ways.

First, you could dive head-first into coding, and try to figure things out as you go. A lot of tutorials and guides can help with this—you'll get something up and running, and while you might be a little hazy on some of the specifics, you'll absorb a lot of it.

Second, you can start with a conceptual understanding of the framework and build up from there, which is the approach my book takes. Start by writing a "vanilla" Node.js HTTP server. Then add Express but only use its middleware feature to see how that works. See how it's like the HTTP server you wrote, but split into chunks. Then add routing, and think about how it's a layer on top of middleware.

About the Book Author

Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects.

 

 

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT