What is ExpressJS ?
Express js is a efficient and easy to learn web application framework. It was written in javascript as a library with different tools which helps you in creating web apps. It was developed by T.J. Holowaychuk, StrongLoop and some contributors under MIT license. This initially released in November 16, 2010. The most stable version is 4.17.2 which released in December 16, 2021. We use Node JS run-time with express to develop web apps. Express work as the back-end web server in the MERN stack application which makes the application fast and robust.
How to install and use Express ?
We need the following tools installed prior to get express.
- NodeJS
- NPM (Node Package Manager)
- Any IDE or Code Editor. (WebStorm / VSCode / Atom / SublimeText)
Download and install the LTS (Long Term Support) versions of these tools and you are ready to go.
For this tutorial I'll use VSCode as our code editor.
Create and open a folder using your editor.
We will create / initialize a nodejs project using npm
.
Open a terminal in your editor in project's root directory.
Enter the command below.
npm init --y
This command will initialize the project with a new file called package.json
which includes the meta data collected from the folder that you created. To change these values we will go to this file and change it. Keywords, author & description etc.
Then we should create the index.js file which is our application's entry point or the startup of our web application's backend. After creating index.js file in the project root we have to change a value in package.json
file. We will change the value of main
to index.js
. Then it should look similar as below.
{
"name": "expressproject",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["express"],
"author": "sndp",
"license": "ISC",
"description": "An express js project"
}
Finally we can enter the following command in the terminal and install express.
npm install express --save
This command will download and add the library to work with your project. Make sure to check package.json to be changed like below.
{
"name": "expressproject",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"express"
],
"author": "sndp",
"license": "ISC",
"description": "An express js project",
"dependencies": {
"express": "^4.17.2"
}
}
We have now successfully installed express in our project.
We will create a simple server that we can use in our project and run it.
Change your index.js
file as below.
const express = require('express')
const server = express();
const port = 5000;
server.listen(port, () => {
console.log("server is listening on port " + port);
})
Now go to the terminal and enter the following command to run our server.
node index.js
If the terminal reads like below with no errors
server is listening on port 5000
Our server is running successfully.
Why developers use expressjs ?
If you are a web developer maybe you are familiar with back-end frameworks like Spring or ASP.Net. But also you must know the express js if you develop a Node JS app. Express is the most common back-end web framework in use for a node js web application in the world today.
Because it is easy
Working with express is easy because it is consist of all javascript based libraries. So the business code and library can work very well together.
Cross-platform
Express can use in a wide variety of platforms. These include mobile app back-end or web application development and many web services. We can route the requests and serve every user with express js.
Communicate with front-end
Express makes it easy to work with front-end because we don't need intermediate libraries. As an example we can use middleware functions in express to direct users before handling the requests using express. And we can use express-session and express-cookies to handle user meta data objects inside server. And there are many more add-ons we can use in our application to implement easier with the front-end.
Uses of express js
There are many uses of building web apps and apis using express js.
To develop web apps
To handle client's requests and handle them by giving responses and directing to the pages or provide implemented templates directly to the user with express. Also it is easy serving and working with static files like stylesheets (css), scripts (js) and images.
Developing ReSTful Web Services (APIs)
Since the progress of web development technologies, apps based on microservices architecture were becoming relevant. From monolithic to microservices architecture they are evolving. A web application basically started after developing an API. Making a API using express js is very easy. It is just a couple of routing methods and connection to a data source away.
Developing other javascript libraries.
Many javascript libraries and frameworks uses express or had used express js in their implementation of libraries. There are many interesting work done with using express framework.
These include,
- Feathers - Build prototypes in minutes,
- ItemsAPI - Search backend for web and mobile apps built on Express and Elastisearch.
- KeystoneJS - Website and API Application Framework etc.
Many companies use express js eg. IBM and UBER in their applications and it is one of the major step of building a node js based web apps.
To learn more on expressjs refer the link below.
Top comments (9)
I'd recommend most people not use express for new projects. It's slower than many alternatives and much less elegant and expressive than most others. The people who wrote express went on to write koa where it's far simpler to compose middleware due to a promise based API. It's kinda sad that so many people cling on to what they left behind and improved upon many years ago.
There may be better alternatives, though I don't think there's an inherit problem with anyone learning express today or implementing it in their projects. I personally learned a lot of new development patterns and concepts from express, and though it may not be the best, it's still a good choice. And if someone thinks they should upgrade, great . Those concepts can easily translate to another framework.
Inflicting yourself and your team with inferior technology is always a problem. It isn't a good choice, any one of the simpler, more powerful and better performing alternatives is a good choice. Express needs to be consigned to history as soon as possible, people only use it because it's already popular, and the more articles written about it the more fuel we put on that fire.
Again, you're point is fine. If you want to chose the best and most efficient tools for your application, awesome. All im saying is theres nothing inheritly wrong with using express. I understand if its a technology thats far phased out of favor in multiple respects and will significantly hinder your application, but with express thats just not the case.
I disagree that making poor technology choices does not constitute a problem.
I also disagree, that wasn't my point. Again, all im saying is express doesn't present significant enought issues in comparison to other frameworks for it to have to be phased out.
I think that express does present significant issues and is a poor technology choice. It's unmaintained and hasn't had a release in 6 years, that's just the beginning of its problems: dev.to/romainlanz/why-you-should-d...
That i'll have to agree with, i wouldn't recommend implementing unmaintained technologies into any production application. I wasn't actually aware of that.
Ill have to stand corrected on that one.