Express 101

Express is a web framework that allows JavaScript programmers to write on the back-end in a language that was originally considered exclusively a front-end language.  Express is layer on top of Node.js that gives it functionality that a web app needs: Middleware!  Routes!  Static file serving!  You could do all of this stuff without Express, but you would have to write a ton more code.  Node.js with express vs. without is like the difference between buying puff pastry form a store vs. trying to make your own.  One is way harder and ends up tasting the same in the end.

Some great things that Express allows you to do:

  • Easily write request handlers!  Request handlers are instructions on what to do when an HTTP request comes in.  Here’s a simple example that says “when you hear a ‘GET’ request for the path ‘/’, send them all the categories.  Screen Shot 2018-11-03 at 9.52.26 PM
  • Export your own modules!  That’s the module.exports part of the code above.  Modules are just JavaScript files (or libraries) that you can import into other files.  Here, I’m exporting my router so that I can use it in other places in my code without having to rewrite everything.  On the other end, I would use the require syntax to import my router.
  • Incorporate middleware!  Middleware are various steps that serve all kinds of useful functions like handling any errors that occur, authenticating users, or logging requests to the console so you can see them as they occur.  Static() is a useful built-in middleware that serves up static files like HTML and CSS files so they’re sent along with any request.
  • Incorporate databases!  Express doesn’t care which database you like, it’ll make it work.  You can require your database and then perform any CRUD (create, read, update, delete) operation on your instances.

So that’s a brief overview of Express!  It’s non-judgmental about a lot of things, like how your organize your files, which modules you use or create, and which database mechanism you prefer.  It’s also bare-bones, meaning that a lot of its power comes from learning which libraries to use to complete certain tasks.  I’m definitely still figuring out my preferred setup, and I’ll let you know as I figure out more of my preferences.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s