2017-05-25 68 views
2

我在嘗試下面的代碼,但它給了我一個錯誤,「res.send不是函數」。請幫幫我。NodeJS res.send不是函數

下面的代碼:

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

服務器運行良好,並連接。正在顯示完整的錯誤是這樣的:

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)

回答

4

按照API reference,第一個參數是請求,然後響應

所以

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

應該修復它。

10

你已經得到了resreq參數周圍的錯誤的方式。

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

應該

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

來源:API docs

1

交換REQ & RES:function(req, res)

+0

工作。謝謝。 – saadi123