What is Express JS, and its Example
What is Express JS
- Express JS is a NodeJS framework. It's the most popular framework as of now ( the most starred on NPM).
- Express JS is a web application framework that provides you with a simple API to build website, web apps and backends.
Why do we actually need Express JS ? How it is useful for us to use with Node.js?
- Try to write a small REST API server in plain Node.js (that is, using only core modules) and then in Express JS. The latter will take you 5-10x less time and lines of code.
get | Read API data |
post | Create API Data |
put | Update |
delete | Delete Api data |
app.get(route, callback)
app.get("/", (req, res)=>{})
- The Callback function has 2 parameters, request(req) and response(res).
- The request object (res) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc.
- Similarly, The response object represent the HTTP response that the Express app sends when it receives an HTTP request.
const express = require("express");// const { route } = require("express/lib/application");const app = express();app.get("/", (req, res)=>{res.send("hello Alians");});app.listen(8000, ()=>{console.log("listening....");});
Express JS routing
const express = require("express");// const { route } = require("express/lib/application");const app = express();app.get("/", (req, res)=>{res.send("hello Alians");});app.get("/about", (req, res)=>{res.send("hello Alians, this is about page");});app.get("/contact", (req, res)=>{res.send("hello Alians, this is contact page");});app.listen(8000, ()=>{console.log("listening....");});
Post a Comment