Javascript Node.js res.send 不是一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44176021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js res.send is not a function
提问by saadi123
I'm trying the following code but it's giving me an error, "res.send is not a function". Please help me.
我正在尝试以下代码,但它给了我一个错误,“res.send 不是函数”。请帮我。
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)
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) 在下一个 (c:\wamp\www\node\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (c:\wamp\www\ node\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [作为 handle_request] (c:\wamp\www\node\node_modules\express\lib\router\layer.js:95: 5) 在 c:\wamp\www\node\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (c:\wamp\www\node\node_modules\express\lib\router\index .js:335:12) 在下一个 (c:\wamp\www\node\node_modules\express\lib\router\index.js:275:10) 在 expressInit (c:\wamp\www\node\node_modules\express \lib\middleware\init.js:40:5) 在 Layer.handle [as handle_request] (c:\wamp\www\node\node_modules\express\lib\router\layer.js:95:5)
回答by Ovidiu Dolha
According to the API reference, the first param always contain request req, then response res.
根据API 参考,第一个参数始终包含 request req,然后是 response res。
So change first param resto req:
因此,将第一个参数更改res为req:
app.get('/', function(req, res) {
res.send("Rendering file")
}
It should fix it.
它应该修复它。
回答by dan
回答by zerek
Swap req & res : function(req, res)
交换请求和资源: function(req, res)
回答by Tabrez Rahi
you should use req argument first and res as the second argument this will work without any issue
你应该首先使用 req 参数,然后使用 res 作为第二个参数,这将毫无问题地工作
app.get('/', function(req, res) {
res.send("Rendering file")
}
回答by Hasan
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!
在某些情况下,您可以改变主意,可以这样写。 注意:默认情况下,当您在本地计算机上运行节点服务器时。你的主机永远是 localhost:"你想要的端口"谢谢!
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}`))

