NodeJS res.send is not a function

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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)

回答1:

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

So

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

should fix it.



回答2:

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.



回答3:

Swap req & res : function(req, res)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!