What Is ExpressJS?
According to the officials ExpressJS Website, Express is a Fast, unopinionated, minimalist web framework for Node.js
Why Use ExpressJS?
Here are some of the features of express
- Web Applications
- Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
- APIs
- With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.
- Performance
- Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.
- Frameworks
- Many popular frameworks are based on Express.
Getting Started
We will need ot create a new folder that will host our express app. We will also need to install afew packages.
create a new folder and cd into it
mkdir express-app && cd express-app
Initialize the project with npm init
npm init -y
Install these packages with npm
npm install express cors dotenv
We will also use apackage called nodemon
to restart our app when we make file changes
npm install -D nodemon
Creating a Server
Creating a server is as simple as creating a file called server.js
and adding the following code to it.
const express = require("express");const cors = require("cors");require("dotenv").config();const app = express();app.use(cors());app.use(express.json());app.use(express.urlencoded({ extended: true }));app.get("/", (req, res) => { res.send("Hello World!");});app.listen(process.env.PORT || 3000, () => { console.log(`Server started on port ${process.env.PORT || 3000}`);});
Now If we visit the app in our browser at https://localhost:3000, we will see the following output
Hello World!