ExpressJS

What is ExpressJS?

Express.js is a Node.js web application framework that provides features for building web and mobile applications. It is one of the most popular frameworks for Node.js due to its simplicity, ease of use, and extensive ecosystem of middleware.

Why ExpressJS?

  • Minimal: Express is a minimalistic framework that does not impose any structure or methodology on applications, giving developers the flexibility to structure their apps as they wish.
  • Middleware: Middleware functions have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. ExpressJS can perform a variety of tasks such as executing code and making changes to the request and response objects, with the help of middleware.
  • Routing: Express provides a routing mechanism to handle routes for our application and specify how the application responds to client requests.
  • Extensible: Express is highly extensible through a wide variety of middleware modules available on npm. This makes it easy to add functionality to our application, such as logging, authentication, and database interaction.

Setting up an ExpressJS Project

  • Since I already have Node.js installed, we start with creating a new project directory.
  • Run npm init to create a package.json file.

npm init -y        

This creates our project.

  • Install Express using npm.

npm install express        

This installs Express as our dependency for our project.

  • Create a server.js file and set up the Express server as below.

const express = require('express')
require('dotenv').config()
const app = express();  
 
const port = process.env.PORT || 8000;

app.use("/", (req, res)=>{
    res.json({
        name:"Aastha",
        age:"19",
        profession:"Student",
        address:{
            temporary_address:"Kathmandu",
            permanent_address:"Jhapa"
        },
        message:"This server is developed by Aastha"});
})

app.listen(port, () => {
    console.log(`Server started on ${port}`);
})        

  • For .env file, we install dotenv as another project dependency.

npm i dotenv        

Once installed, we create a .env file which stores our environment variable. In this case,

PORT=5000        

  • Run the server.

node server.js        

Thanks to our mentor, Bikash Karki , for his insightful lecture.

要查看或添加评论,请登录

Aastha S.的更多文章

  • Day 13: Forms in React (Formik and Yup)

    Day 13: Forms in React (Formik and Yup)

    On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application…

  • Day 12: localStorage

    Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 8:React Hooks (Part 2)

    Day 8:React Hooks (Part 2)

    After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in…

  • Day 7: React Hooks

    Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 6: Dynamic React Components using Props

    Day 6: Dynamic React Components using Props

    React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A…

  • Day 5: React Layouts, Outlets, and Links

    Day 5: React Layouts, Outlets, and Links

    On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are…

  • Day 4: React-Router-DOM and BootStrap

    Day 4: React-Router-DOM and BootStrap

    On the fourth day of learning React, we learned about installing Bootstrap and React router DOM, all thanks to our…

社区洞察

其他会员也浏览了