Jack Shepherd
Jack Shepherd

Reputation: 393

node.js TypeError: undefined is not a function on app.get("/")

I'm trying out node.js and some hello world examples and I'm getting this error

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)

Here's my code

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

I'm only getting the error when I hit http://localhost:3000/

I do not get any error when I hit http://localhost/api/users

Upvotes: 0

Views: 1191

Answers (2)

Sagar Maurya
Sagar Maurya

Reputation: 74

Your function for app.get("/") is not correct. The order in which you have passed the arguments is not right. The correct way is to first give request object and then the response object. The correct way is to write like this:

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

Hope this might work for you. :)

Upvotes: 1

Anshuman Singh
Anshuman Singh

Reputation: 1152

problem in a ordering of parameter.. req object doesn't have any send function.

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

Upvotes: 3

Related Questions