Express.js is an open-source server-side and mobile API application framework for Node.js. Node.js by itself requires a lot of repetitive and automated actions which is sometimes very time consuming and cumbersome. Express.js simplifies the process of development by offering a set of useful tools, features, packages and plugins. Express.js is a minimalist and flexible framework, it gives you a freedom to develop an application of your own preference. In this article we will dive deeper into Express.js advantages and disadvantages, performance, and examples of Express.js apps.
Advantages of Express.js
Before highlighting the benefits of Express.js, let's read what developers like about it.
Fast app development
I/O request handling
Express JS is a great choice for applications that handle a lot of requests and notifications from users. Exactly because of this reason Uber has chosen Node.js. This is what the Software Engineer of Uber said: “Node.js is particularly well-suited to writing systems that have all their state in memory. They do not have to externalize the concerns of a distributed system.
As a consequence, the systems can be more available, and they can respond more quickly to requests by eliminating the reading/writing and the serialization of state into a database”. To conclude, Node.js together with Express.js is capable of supporting thousands of concurrent actions.
Minimizing the expenses spent on hiring the development team
Node.js simplifies the process of developing mobile apps and makes it more efficient. You can use the same language to develop your server and mobile app.
Express.js is one of the most supported Node.js frameworks. It has an open-source community, so the code is always reviewed and improved. TJ Holowaychuk, the founder of the Express.js stated: “There is no way I could have learned everything that I have without open-source. Contributions and feedback you get on your own code are really helpful. Naturally it feels great to have people enjoy something you have created as well, so that can be fun”.
Easy integration of third-party services and middleware
Express.js is a minimalist framework, so Express development team has created middleware packages for solving different development problems. They include URL parameters, sessions, POST data, security headers, libraries etc. As Express.js is unopinionated framework, which means that there is no strict and determined rules on how to deal with particular tasks or what components to choose. You can use any middleware in the order that is convenient for you. Also, you can decide how to structure your app, as there is no one right way to do it.
Easy to learn
How it Works
Here is a block scheme on how Express.js works with Node.js and MongoDB
Node.js is an event-driven server, that has a single thread which manages all connections to the server. It processes all the requests in the callbacks without delaying their flow. As you can see from the picture above, when the request enters the server, Express processes it into the callback without blocking the main stack.
Imagine that you need to retrieve some data from the file or database. Such process may be time-consuming (for example because of disc read delay). The average server will block all other requests until these data are delivered. Node JS, in its turn, registers the logic of data post-processing in the callback. Data are processed in parallel, so the server can accept another data at the same time. As the result, Node.js can process the bigger amount of requests more efficiently.
Each request in MongoDB is processed in the same way as in the callback. It means that the server doesn't have to wait (block the stock) until the certain data are transferred and can process other requests simultaneously. It is extremely important because the scale-out database can be situated on one or several physically remote servers, so retrieving data from them is time-sapping.
Now let’s conclude the whole process once again. Server receives the request and registers the event data processing. Then the event sends the request to the remote Mongo server. After this is done, some time is needed for data to return. Simultaneously, the server is processing other requests. When the data arrives, Mongo driver invokes the user request handler. The user request handler delivers the data back to the client.
Express Rest API
- API server
Clients may include both web platforms and mobile apps. Web platforms can be written with any development technology (e.g. React, Backbone.js). Mobile apps may be developed with ReactNative that supports both Android and iOS platforms or with native programming language.
API server processes all the requests from the users. Express.js perfectly suits for systems with a large flow of inputs and outputs. All the data is processed and saved in the database with Mongo Driver.
Clients request the data from the server through the API. The server fetches the data from the database and returns it to the client through the API. After that, clients display these data.
It goes without saying that performance is the most important aspect of the app. Express.js by itself is the Node.js framework for code structuring and it does not influence the performance in general. That is why we will compare the performance of Node JS server to other servers such as ASP. NET and Java.
Node.js vs Java
This what Jeff Harrell, the lead of PayPal UI Engineering Architecture said about this choice: “It unifies our engineering specialties into one team which allows us to understand and react to our users’ needs at any level in the technology stack.” To check the performance of Node.js app, PayPal engineers started to develop two apps: one in Java and another in Node.js. Here are the results of their comparison:
The requests per second were doubled in comparison with the Java application and the average response time was decreased by 35% for the same page which resulted in pages being served 200ms faster.
So, Node.js definitely wins with speed. The data come in and out very fast, as Node.js together with Express is capable of handling a huge number of simultaneous connections and concurrent actions.
Node.js vs ASP.net
Although Express.js is a very convenient and easy to use framework, it has some minor drawbacks that may influence the development process. Before starting the development with Node.js or Express.js think about the scale and complexity of the project. Here are the main underlying potential problems you may face and some ways to avoid them.
Event-driven nature (callbacks)
Node.js is a single threaded framework with event loop that listens to various events and executes registered callbacks. Developers who have worked with other programming languages may find it difficult to understand the callback nature. After reading several instructions, they start to write all the code in callbacks. Such approach will only entangle the code, so it will be impossible to maintain it in future. This is called a callback hell and we hope you will never experience it! The last version of Node.js gives you the opportunity to use async/await , that will increase the code quality and prevent you from the problems with callbacks.
Philosophy of plugins known as middleware
Express.js is built on this philosophy, that is why it is important to understand its main concepts. In short, middleware is a subset of chained functions that run between the client request and the server answer.
Note that the code organization in Express.js is represented by patterns that make your code easier to maintain. Here are some useful tips for simplifying the development process.
Choose one approach and stick to it during the whole process of development
Control the size of your files. If they are too big, divide them into several (100 lines)
Stay tuned about the latest Node.js updates and Express.js tendencies
Types of apps, where Node.js+Express.js is the Right Choice
Applications, that communicate with third party resources
As we already mentioned, Node.js can handle a large amount of simultaneous connections, so it will be useful for server-side applications interacting with third-party resources and gathering data from them.
Applications for monitoring the visitor's actions
Node.js is a great choice for building the back-end of applications for tracking website visitors and visualizing the outcoming data. For example, you need to investigate reviews about your company as soon as they appear, keep track of all ratings and analyze them. JSSolutions has developed such project for gathering reviews from different websites in one application. We decided to build it with Node.js exactly because of the reasons, mentioned above.
Node.js allows to easily build applications that are updated in real time. Here is a nice case by Joe Lennon, who decided to develop a real time polls application that will display voting results, updating in real time. He has chosen this technology because of its simple architecture and scalability. In addition, Node.js is an event-based and non-blocking driven server that can handle a large number of real-time users.
Examples of apps built with Express.js
Here is the list of Express.js app examples (open-source projects on Github) and their short overview.
This is an open source framework built in Express.js and MongoDB for developing database driven-websites, apps and APIs in Node.js. It provides you with the most essential features such as database fields, auto-generated admin UI, email sending and integration with Mandrill, session management, dynamic templates and views, connection to your MongoDB database etc. Keystone is very flexible, so you can use features you need and replace or remove those you don`t.
Ghost is an open-source professional publishing platform for building and managing online publication. It provides you with the suite of publishing tools, optimized distribution channels and simple content management system. This application was downloaded 1,408, 153 times by developers from different countries. It uses a modern technology stack with flexible components and is developed on Github.
This is an open-source absence management software for small and medium size businesses. It gives complete freedom to customers and allows to use it within your own infrastructure and can be deployed into the customer service without any enquires. Its main features include: multiple views of staff absence, third party calendar integration, access control with different types of users, data migration between different installations etc. There are many new upcoming features, and anyone can make suggestions on how to improve it.
Hexo is a simple blog framework powered by Node.js. Its main features are: fast file generating, one-command deployment to Github pages, markdown support and a powerful plugin system.