Http Module nodejs

Afroj MansooriAfroj Mansoori
1 min read

Bui:d a Server Using Http Modu:e Node<S with api endpoints for Getting Products Dat9 " ‘/’G →G send msg as “We:come to Men & Women Dummy Data”F " G‘/men’ →G send 10 products data of menF " ‘/women’G →G send 10 products data of wome; " ‘/other’G → send response as page not found Hin#: You can use any data, send data

const mendata=[{"id":1,"name":"t-shirt","brand":"polo","price":400,"color":"red","size":"M"},
{"id":1,"name":"t-shirt","brand":"polo","price":400,"color":"red","size":"M"},{"id":2,"name":"t-shirt","brand":"polo","price":500,"color":"white","size":"M"},{"id":4,"name":"t-shirt","brand":"puma","price":400,"color":"red","size":"L"},{"id":5,"name":"t-shirt","brand":"polo","price":400,"color":"yellow","size":"M"},{"id":6,"name":"t-shirt","brand":"Levis","price":300,"color":"blue","size":"S"},{"id":7,"name":"t-shirt","brand":"zara","price":350,"color":"black","size":"M"},{"id":8,"name":"paint","brand":"mufti","price":4000,"color":"blue","size":"28"},{"id":9,"name":"shirt","brand":"Calvin kare","price":200,"color":"red","size":"M"},{"id":10,"name":"Kurta","brand":"RAymand","price":2300,"color":"white","size":"X"}];

const womendata=[{"id":1,"name":"Kurti","brand":"ZARA","price":1800,"color":"Pink","size":"L"},
{"id":1,"name":"huddi","brand":"henm","price":400,"color":"red","size":"M"},{"id":2,"name":"laggi","brand":"Messi","price":500,"color":"white","size":"M"},{"id":3,"name":"t-shirt","brand":"puma","price":400,"color":"red","size":"L"},{"id":4,"name":"t-shirt","brand":"polo","price":400,"color":"yellow","size":"M"},{"id":5,"name":"t-shirt","brand":"Levis","price":300,"color":"blue","size":"S"},{"id":6,"name":"t-shirt","brand":"zara","price":350,"color":"black","size":"M"},{"id":7,"name":"paint","brand":"mufti","price":4000,"color":"blue","size":"28"},{"id":8,"name":"shirt","brand":"Calvin kare","price":200,"color":"red","size":"M"},{"id":9,"name":"Kurta","brand":"RAymand","price":2300,"color":"white","size":"X"}];

const http= require('http')
const PORT=3006;
 const server=http.createServer((req,res)=>{
    if(req.url=='/'){
        res.end(" <h1>Welcome to men and women dummy data:</h1>")
    }
    if(req.url=='/men'){
        res.end(JSON.stringify(mendata));
    }
    if(req.url=='/women'){
        res.end(JSON.stringify(womendata));
    }
 })
 server.listen(PORT);

in json format.

Q.3_Create a Basic Server with Different Routes using Expresl " /G G → send response as {msg:I am homepage^ " /aboutG → send response as {msg:I am about page^ " /contact → send response as {emai::suppor#@pwskills.com}

const express = require('express');
const app = express(); //instance
PORT=3000;
app.get('/',(req,res)=>{
    res.send("I am Homepage");
})
app.get('/about',(req,res)=>{
    res.send("I am About page")
})
app.get('/contac',(req,res)=>{
    res.send("support@pw.com")
})

const Hostname='localhost';
app.listen(PORT,()=>{
    console.log(`server is running at ${Hostname}:${PORT}`)
});

0
Subscribe to my newsletter

Read articles from Afroj Mansoori directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Afroj Mansoori
Afroj Mansoori