2.6 Writing Node with Frameworks


What is a Framework?

While Node.JS itself is a javascript runtime environment (a place developers can run programs), many frameworks have been created for Node.js. A framework is a collection of pre-built modules and tools that provide a structure, and are often necessary, for developing web/ software applications. Frameworks usually help developers structure their web applications, handle requests, perform common tasks, and manage data.
In node, a framework is a platform for developers to create applications using JavaScript for both front-end and back-end development. There are a large number of Node frameworks built off of Node.js which extend its original functionalities.
Benefits include:
  • Provided set of conventions and best practices across the team
  • Higher scalability and more easily maintainable applications
  • Allows use of the same programming language for both front-end and back-end
  • High speed due to use of the V8 JavaScript engine
Modular structure (framework’s code is organized into small, reusable modules) allows for easier access and changing of the codebase.

Types of frameworks

In Node.js, there are mainly three types of frameworks: MVC, Full-Stack MVC, and REST API frameworks.
MVC: MVC frameworks are based on a design pattern splitting application logic into three parts: Model, View, and Controller.
The Model represents the data and the business logic of the application and the View represents the user interface and data presentation. The Controller works as an intermediary between the Model and the View and handles user input. The goal of the MVC pattern is to separate the layers of the application and increase ease of scalability and maintainability.
Full-Stack MVC: Full-Stack MVC frameworks are built on top of the MVC pattern and offer additional features including routing, scaffolding, template engines, and more. These frameworks help in building complex and large-scale applications.
REST API: REST API frameworks, as the name suggests, are designed to build RESTful application programming interfaces. These frameworks provide a set of tools and modules to create, test, and implement RESTful APIs. These usually include routing, request and response handling, and more features of building API’s as well as a prepared programming interface, saving time for building apps requiring an internet connection.

Common frameworks


Also known as Express, this is the most popular framework for web applications with Node. It was officially released in 2010 under the MIT license and provides a minimalistic web framework. Express includes a small and flexible set of features for web and mobile applications, making it beginner friendly.
Combined with a database to form the backend of the application, the Express framework can greatly shorten the amount of code needed to write a server by managing functions such as handling requests, saving sessions, payload parsing, and routing.
Express is used in applications by companies like Twitter, Uber, IBM, and more.
To install Express on a system (after installing node), we can write a command in the terminal or command prompt with “npm install express.”
The express() function then creates an Express application exported by the express module.
notion image
A few example commands include:
  • get('/', (req, res) => res.send('message')) - Defines a GET request handler for the root route that sends a response “message”.
  • listen(#, () => console.log('app listening on port #')) - Tells the Express.js server to listen on port 3000 and log a message to the console when it’s ready.
  • use('path', function) - Use a custom route handler for all requests starting with /path.
  • set(name, value) - assigns a value to a name.
There is a wide range of commands, middleware, and plugins available outside the scope of this course.
When to use
Express is useful in nearly any web application development, especially for beginners.
Another of the more popular Node.js frameworks, Koa is similar to Express. The key difference is that it is more lightweight and highly modular, allowing developers to add only the functionality they need for their application. This makes Koa a popular choice for building microservices, RESTful APIs, and single-page applications.
Koa uses generators (functions that can be exited and reentered), async, and await to handle asynchronous operations thereby reducing the amount of boilerplate code required for common tasks. Other key features of Koa include effective management of HTTP middleware and concise and customizable API building.
When to use
Koa is great for high demand on performance and large apps, especially with experienced development teams. However as it requires more manual coding than Express and has less of a community around it, it may be less beginner friendly.
Officially launched in 2012, Meteor is an open-source, isomorphic, full-stack framework for building real-time web applications with Javascript. It propagates automatic data changes to clients without developer’s efforts. It supports real-time data updates to clients, hot code pushes, and can operate on many variants of operating systems. However, a large size and performance issues may arise from this.
When to use
Meteor.js is better with more javascript experience and for cross-platform apps as it allows the same code for both a web and mobile app.
Sails.js is a real-time, MVC framework for building modern Node.js applications. It is designed to be simple, flexible, and scalable, and provides a strong set of features for building web and mobile applications. It is compatible with all databases and the most suitable framework for high-end customized applications. As well, Sails allows integration with npm modules and the sharing of API’s with other web services/ development teams.
When to use
Its compatibility with Socket.io, a Javascript library used for establishing bidirectional communication between web clients and servers, and recyclable middleware functions, make Sails helpful in building social media, chat, and gaming applications. Sails is desirable in large custom Node.js applications and can build production-ready apps relatively quickly. It may, however, be too heavy for small projects.

Previous Section

Next Section

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.