Home > Net >  Showing res.json is not a function
Showing res.json is not a function

Time:02-05

    const Express=require('express');
    const fs=require('fs');
    const app =new Express();
    // app.get("/",(req,res)=>{
    //     res.send("Hello I am at Home");
    // })
    // app.post("/",(req,res)=>{
    //     res.send("hello i am at end point");
    // })
    const tours=JSON.parse(
        fs.readFileSync(`${__dirname}/data.json`,'utf-8')
    )
    app.get('/api',(res,req)=>{
        res.json({
            status:"success",
            data:{
                tours:tours
            }
    
        })
        console.log("server running")
    })
    app.listen(8000);

Showing res.json is not a function.

Why it is showing this error? I saw a tutorial in which the code run successfully, but I am getting error?

CodePudding user response:

try this code for the route

app.get('/api', (req, res) => {
    res.json({
        status:"success",
        data:{
            tours:tours
        }

    })
    console.log("server running")
})

It should be (req, res) not (res, req)

CodePudding user response:

your callback parameter is reversed. it should be :

app.get('/api', (req, res) => {
... 
})
  •  Tags:  
  • Related