Start United States USA — software What is Express.js and Why Does It Matter?

What is Express.js and Why Does It Matter?

278
0
TEILEN

In a world of monolithic web frameworks, Express.js offers an alternative. It is described as a „fast, unopinionated, minimalist server-side web framework for Node.js.“ and strives to make web application development for Node.js easier. This article breaks down exactly how Express works.
In a world of monolithic Web frameworks, enterprise-level solutions, and deep toolkits for every language, defaulting to the largest hammer for even the smallest nails is easy. Enter Express.js, described on its website as a „fast, unopinionated, minimalist server-side Web framework for Node.js .“
Written in JavaScript, Express acts only as a thin layer of core Web application features. Unlike a big, highly opinionated framework like Ruby on Rails, Express has no out-of-the-box object relational mapping or templating engine. Express isn’t built around specific components, having „no opinion“ regarding what technologies you plug into it. It strives to put control in the developer’s hands and make Web application development for Node.js easier. This freedom, coupled with lightning fast setup and the pure JavaScript environment of Node, makes Express a strong candidate for quick development and rapid prototyping. Express is most popular with startups that want to build a product as quickly as possible and don’t have very much legacy code.
Let’s break down exactly how Express works. To understand Express, first you have to understand Node.js.
The Node.js environment is a key part of what makes Express so easy to build and deploy. Node can be thought of as a cross-platform JavaScript interpreter, able to run JavaScript free from the confines of the browser. In short, Node allows developers to write server-side code for Web apps and networking tools using JavaScript instead of another server-side language like PHP, Python, Java, etc. Using Node Package Manager, or npm, you can install libraries like Express to customize an existing Node installation to your needs. Node users have made thousands of these open-source libraries and server frameworks available. Check out this simple Web server:
With just a couple lines of code and the Express library, you can build a simple server that will talk to browsers over localhost: 8000.
By bringing JavaScript to the server side, Node benefits developers in two major ways. First and foremost, an all-JavaScript Web stack could cut development time significantly. According to Stack Overflow’s 2017 Dev Survey, more Web developers use JavaScript than any other technology. It’s growing too, especially with the popularity of front-end frameworks like React and Angular on the rise. When you write in JavaScript, you write a code base that more developers can understand. It is the world’s most popular Web technology, so it has an endless stream of communities, support, and insight into its development. The ability to write both your front end and back end in a well-supported language means your dev team will be more productive and the final product gets done faster.
Secondly, developers benefit from JavaScript’s nature as a non-blocking event-driven language. Think of the bulk of all Web traffic as events; JavaScript can handle all that concurrent traffic on a single thread by using an event loop. Essentially, JavaScript is extremely good at staying busy. When a request goes out for data from another source, instead of sitting motionless and waiting for the reply, JavaScript continues with its other tasks. JavaScript will stay ready to complete the action when the response arrives. All of this happens only on one processor thread, making Node less taxing on the server’s hardware.
How does Express fit into all this? First and foremost, Express gives developers all the tooling they need to actually build HTTP servers. Node.js does very little out of the box. Node is an environment, it’s just a foundation. Express is what gives you access to both incoming and outgoing Web traffic, but stops short of enforcing an object relational mapping, database, or templating engine.
This capability makes life easier for developers because it gives them freedom. Express isn’t the only Node module in town, and you can get components for virtually any need. Instead of being locked into a particular style of templating, Express will work with Handlebar.js, Jade, ejs, and more. Just as easily (and perhaps of more interest to ProgrammableWeb’s API provider-minded audience) , you could completely exclude a templating component and turn your Express application into an API server, spitting out nothing but JavaScript Object Notation (JSON) data.
With data storage, you get this same freedom. Using a Node module called Mongoose, you can attach MongoDB to your Express app. You can plug Redis, SQL, Postgres — all the industry leading technologies — straight into your app with just a tiny layer of abstraction. Here, developers can make choices regarding what best works for their current application and avoid lengthy workarounds. For example, Django does not work with non-relational databases like MongoDB. Workarounds do exist, but usually lack official support, have limited functionality and a much higher cost in development time.
Developers can configure Express to the exact needs of their application, adding and removing components as necessary. You can build everything from static-content servers to RESTful APIs to full Model-View-Controller (MVC) frameworks built from scratch to lean lightweight microservices. The minimalism and un-opinionated nature of Express really comes to light here. It provides very little to a very rich environment, but what it does give is the basis of all Web traffic. Then, developers are free to integrate additional components and features as they need them.
Now, this isn’t without its downsides. Minimalistic by nature, Express apps might make jumping between one team’s app to the next difficult. With all the freedom in configuration and setup, the code base belong to one team could end up looking very different from the code base belonging to another. This also creates a new barrier for entry. It doesn’t matter if a large framework enforces only a single flavor of database if that’s the only kind of database a particular team knows. Express will still require developers well-versed JavaScript and Node who can pick up new technologies quickly.

Continue reading...