This content originally appeared on DEV Community 👩💻👨💻 and was authored by Sean Niehus
Express bills itself server-side framework as 'fast, un-opinionated and minimalist web framework for Node.js.' Express's bare-bones skeleton gives the user the ability to define its structure, making it the ideal option to use in conjunction with client-side frameworks to create dynamic apps and websites with ease. In this article, I will show you how to get started using the framework and talk about some of the features that make it the most popular node framework.
Getting Started
The below example from the Express website shows you how simple it is to get started using it on a project:
source: https://expressjs.com/en/starter/hello-world.html
The first few lines of the above code, in addition to installing the node package manager(NPM) in the terminal (along with having Node installed), are all that's needed to begin. The function on lines 5 through 7 is typical of the way functions will be created which we will examine next. Finally, on line 9 the code here defines the port to listen on, where you will go to find the output.
Middleware Functions
Express offers users loads of ways to write clean code and move data to and from the front-end to the back-end (and has a ton of resources on their website), here I will just scratch the surface of its capabilities. Middleware functions are the bread and butter of the framework, giving you the ability to manipulate the properties of the request and response objects, using .get, .post, .put and .delete. In the graphic below (from Express's website) you can see the format and the anatomy of one of these function calls:
source: https://expressjs.com/en/guide/writing-middleware.html
After the .get method is defined, the route to the context of the function is declared, defining the path to the target file. Next, in the invocation of the anonymous function, the first two arguments are for the request and response arguments followed by a callback which is executed in the body of the function. Middleware functions can be easily created with just a few lines of clean, concise code.
Express Application Generator
One last asset that I will touch on is the express-generator which offers a fast, efficient way to set up a custom framework for an app pre-configured with the Express capabilities of your choosing. The beauty of Express is its minimalist framework that will not limit what you want to do with it, it doesn't dictate the terms, but gives you only the capabilities that you want. The generator allows you to create, customize and reuse frameworks that will allow you to get your project off the ground in very little time. Using a compatible template engine is also a possibility for efficiently setting up a skeleton framework.
Express has so many great features, it's easy to see why it is so popular. The capabilities discussed above are just a few of the reasons that it is the go-to framework for so many developers.
This content originally appeared on DEV Community 👩💻👨💻 and was authored by Sean Niehus
Sean Niehus | Sciencx (2022-10-03T22:43:45+00:00) A Brief Introduction to Express.js. Retrieved from https://www.scien.cx/2022/10/03/a-brief-introduction-to-express-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.