Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 388
saadi123 Avatar asked May 25 '17 08:05

saadi123


People also ask

What does Res send () do?

send() Send a string response in a format other than JSON (XML, CSV, plain text, etc.). This method is used in the underlying implementation of most of the other terminal response methods.

What is RES status?

The res. status() function set the HTTP status for the response. It is a chainable alias of Node's response.

What is RES end in node JS?

res. end() will end the response process. This method actually comes from Node core, specifically the response. end() method of http. ServerResponse .


1 Answers

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.

like image 153
Ovidiu Dolha Avatar answered Oct 05 '22 19:10

Ovidiu Dolha