Node.js res.send is not a function

I am trying the following code, but it throws "res.send is not a function" error. Please help me.

Here is the code:

var http = require('http');
var fs = require('fs');
var connect = require('connect');
var express = require('express');

var app = express();
app.get('/', function(res, req  ) {
        res.send('Hello World');
    });

var server = app.listen(8888, function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("Example app listening at http://%s:%s", host, port);
});

      

The server is working fine and connecting. The complete error that is displayed looks something like this:

Error like: res.send is not a function in c: \ wamp \ www \ node \ server.js: 8: 13 in Layer.handle [as handle_request] (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ layer.js: 95: 5) on the following (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ route.js: 137: 13) on Route.dispatch (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ route.js: 112: 3) in Layer.handle [as handle_request] (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ layer.js: 95: 5) in c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ index.js: 281: 22 in Function.process_params (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ index .js: 335: 12) in the following (c: \ wamp \ www \ node \ node_modules \ express \ lib \ router \ index.js: 275: 10) in expressInit (c: \ wamp \ www \ node \ node_modules \ express \ lib \ middleware \ init.js: 40: 5) in Layer.handle [as handle_request] (c:\ wamp \ www \ node \ node_modules \ express \ lib \ router \ layer.js: 95: 5)

+11


source to share


4 answers


According to the API reference , the first parameter is the request, then the response

So



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

      

should fix it.

+29


source


You got parameters res

and req

wrong.

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

      

should be



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

      

Source: API docs .

+18


source


Change req and res: function(req, res)

+5


source


first you have to use the req argument and the second argument res - this will work without issue.

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

Run codeHide result


0


source







All Articles