saadi123
saadi123

Reputation: 614

Node.js res.send is not a function

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('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 running fine and is connecting. The complete error that is being displayed is something like this:

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

Upvotes: 42

Views: 98157

Answers (7)

Huzaifa Iftikhar
Huzaifa Iftikhar

Reputation: 41

Everything fine. Good Work. Just make a little change in parameters e.g (req, res) instead of (res, req). Its a Rule.

Upvotes: 3

KailashJS
KailashJS

Reputation: 11

The middleware function you are writing is a callback function and should follow the order of declaring params function(req, res, next) req for request res for response and next for further operation. You can refer to API reference.

So the proper code would be:

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

var app = express();
app.get('/', function(req, res) {
        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);
});

Upvotes: 0

Ovidiu Dolha
Ovidiu Dolha

Reputation: 5413

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.

Upvotes: 101

Hasan
Hasan

Reputation: 41

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}`))

Upvotes: 0

Tabrez Rahi
Tabrez Rahi

Reputation: 1

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")
}

Upvotes: 0

dan
dan

Reputation: 1984

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.

Upvotes: 27

zerek
zerek

Reputation: 253

Swap req & res : function(req, res)

Upvotes: 6

Related Questions