node.js TypeError:undefined不是app.get上的函数(" /")

时间:2016-07-06 05:10:16

标签: node.js express

我正在尝试node.js和一些hello world示例,我收到此错误

TypeError: undefined is not a function
   at c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\server.js:10:13
   at Layer.handle [as handle_request] (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\layer.js:95:5)
   at next (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\route.js:131:13)
   at Route.dispatch (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\layer.js:95:5)
   at c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\index.js:277:22
   at Function.process_params (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\index.js:330:12)
   at next (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\index.js:271:10)
   at expressInit (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\middleware\init.js:33:5)
   at Layer.handle [as handle_request] (c:\users\admin\documents\visual studio 2015\Projects\TheBoard\TheBoard\node_modules\express\lib\router\layer.js:95:5)

这是我的代码

var http = require("http");
var express = require("express");
var app = express();

app.get("/",
    function(res, req) {
        res.send("<html><body><h1>Express</h1></body></html>");        
    });

app.get("/api/users",
    function(req, res) {
        res.send({ name: "Louis", isValid: true, group: "Admin" });
    });

var server = http.createServer(app);

server.listen(3000);

我在点击http://localhost:3000/

时才收到错误消息

点击http://localhost/api/users

后,我没有收到任何错误消息

2 个答案:

答案 0 :(得分:3)

参数排序中的问题.. req对象没有任何发送功能。

var http = require("http");
var express = require("express");
var app = express();

app.get("/",
    function(req, res) { 
        res.send("<html><body><h1>Express</h1></body></html>");        
    });

app.get("/api/users",
    function(req, res) {
        res.send({ name: "Louis", isValid: true, group: "Admin" });
    });

var server = http.createServer(app);

server.listen(3000);

答案 1 :(得分:1)

app.get(&#34; /&#34;)的功能不正确。您传递参数的顺序不正确。正确的方法是首先提供请求对象,然后提供响应对象。正确的方法是这样写:

app.get("/",
    function(req, res) {
    res.send("<html><body><h1>Express</h1></body></html>");        
    });

希望这对你有用。 :)