Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 21, 2021 11:02 am GMT

Introduction to Expressjs

Introduction

In this blog article, we'll learn how to set up express and run an http server instance using the express library.

Expressjs is a non-opinionated library for setting up an http server for a REST api or web backend. Express never puts restrictions on how to set up your project.

Express is very lean, with no third-party libraries preinstalled. This makes Express powerful because express is easily extesible using middlewares.

Middlewares are functions that have access to the request and response objects. Middlewares intercept requests to your application and can check, validate, modify or read values from the request and response objects.

With that said, let's set up a simple Expressjs application.

1. create a new project

To create a new project, run the following command to create a new directory and initialize a project. You may create the directory manually by going inside the directory, create a New folder, give the folder a name, then using Powershell run npm init -y to initialize the project.

mkdir express-introcd express-intro## initialize a Nodejs projectnpm init -y

This creates an empty Nodejs project with a package.json file that will hold project metadata and scripts

2. Install express

Install express dependency from npm

npm i express

3. Create app.js

Create a file named app.js, the name does not have to be app.js, you can choose any meaningful name for the file.

  1. Import the express dependency:
const express = require('express')
  1. Initialize your application
const app = express()
  1. Declare a port, which the http server will listen for requests on
const PORT = process.env.PORT || 3000

Ideally, we'd want to read the value of PORT off process.env.PORT if not available, use 3000. This is good practice so that we don't run into issues in production

  1. create a route

A route maps to a resource on the http server tha we will run using express.

on a seperate line:

app.get('/hello-world' , (req, res) => {  res.send('hello word!')})// run the http server on PORT 3000app.listen(PORT, () => console.log(`app runinng at port ${PORT}`))

Download project code here: intro-to-express

This route will map to: http://127.0.0.1:3000/hello-world

Visiting this route on the browser:

Hello world

Explanation

When we visit the /hello-world route, we initiate a GET request to our http server. When the request reaches the server the get() method is called on our app instance, passing in the /hello-world route name, and a callback function that has access to request and response objects.
Inside the callback, nothing special happens, we send back a response to complete the request-response cycle.

This is a a sequel to tutorial serries on the Expressjs framwork. We shall cover the following areas:

  1. Setting up express
  2. Routing
  3. Middlewares
  4. Security practices### SummaryExpress supports more http verbs such as:
  5. POST
  6. PATCH
  7. DELETE

Found this article helpful? you may preorder Modern JavaScript Primer to improve and help better understanding of Modern JavaScript.

Merry Christmas


Original Link: https://dev.to/nmurgor/introduction-to-expressjs-4je7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To