Node.js res.send is not a function

前端 未结 5 1003
暗喜
暗喜 2020-12-16 11:01

I\'m trying the following code but it\'s giving me an error, \"res.send is not a function\". Please help me.

Here\'s the code:

var http = require(\'         


        
相关标签:
5条回答
  • 2020-12-16 11:18

    Swap req & res : function(req, res)

    0 讨论(0)
  • 2020-12-16 11:19

    According to the API reference, the first param always contain request req, then response res.

    So change first param res to req:

    app.get('/', function(req, res) {
        res.send("Rendering file")
    }
    

    It should fix it.

    0 讨论(0)
  • 2020-12-16 11:21

    You can change your mind in some cases and could be write like this. note: by default, when you run node server on your local machine. Your host will always be localhost:"your desire port" Thank you!

    const express = require("express")
    const app = express();
    const port = 8888;
    // for redering hello you don't need to require fs
    const fs = require("fs")
    
    app.get ('/', (req, res) => {
        res.send("hello world")
    })
    
    
    app.listen(port, () => console.log(`Listening on ${port}`))
    
    0 讨论(0)
  • 2020-12-16 11:25

    you should use req argument first and res as the second argument this will work without any issue

    app.get('/', function(req, res) {
     res.send("Rendering file")
    }

    0 讨论(0)
  • 2020-12-16 11:33

    You've got the res and req parameters the wrong way around.

    app.get('/', function(res, req)
    

    should be

    app.get('/', function(req, res)
    

    Source: API docs.

    0 讨论(0)
提交回复
热议问题