I want to do basic express routing. But it return -> Error: Route.get() requires a callback function but got a [object Undefined]
Motive: basic express routing. since there are many files in one directory, i want this directory path handled by variable. But the other file handled sparately. So when i code the path, it will require file name only. This will make easy coding off course.
Lets see i have main file app.js located on "./ "
and here i code:
var next = require('./routes');
app.get('/', next.index);
The path is handled by variable next. I also have file index.js located on "./routes "
and here i code:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next)
res.render('index', title: 'Express' );
);
module.exports = router;
Instead of doing respond, it return error as ->
Error: Route.get() requires a callback function but got a [object Undefined]
But when i replace next variable as follow from app.js:
var next = require('./routes/index');
app.get('/', next);
It work. So where do i need to change?
javascript node.js express
add a comment |
Motive: basic express routing. since there are many files in one directory, i want this directory path handled by variable. But the other file handled sparately. So when i code the path, it will require file name only. This will make easy coding off course.
Lets see i have main file app.js located on "./ "
and here i code:
var next = require('./routes');
app.get('/', next.index);
The path is handled by variable next. I also have file index.js located on "./routes "
and here i code:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next)
res.render('index', title: 'Express' );
);
module.exports = router;
Instead of doing respond, it return error as ->
Error: Route.get() requires a callback function but got a [object Undefined]
But when i replace next variable as follow from app.js:
var next = require('./routes/index');
app.get('/', next);
It work. So where do i need to change?
javascript node.js express
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25
add a comment |
Motive: basic express routing. since there are many files in one directory, i want this directory path handled by variable. But the other file handled sparately. So when i code the path, it will require file name only. This will make easy coding off course.
Lets see i have main file app.js located on "./ "
and here i code:
var next = require('./routes');
app.get('/', next.index);
The path is handled by variable next. I also have file index.js located on "./routes "
and here i code:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next)
res.render('index', title: 'Express' );
);
module.exports = router;
Instead of doing respond, it return error as ->
Error: Route.get() requires a callback function but got a [object Undefined]
But when i replace next variable as follow from app.js:
var next = require('./routes/index');
app.get('/', next);
It work. So where do i need to change?
javascript node.js express
Motive: basic express routing. since there are many files in one directory, i want this directory path handled by variable. But the other file handled sparately. So when i code the path, it will require file name only. This will make easy coding off course.
Lets see i have main file app.js located on "./ "
and here i code:
var next = require('./routes');
app.get('/', next.index);
The path is handled by variable next. I also have file index.js located on "./routes "
and here i code:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next)
res.render('index', title: 'Express' );
);
module.exports = router;
Instead of doing respond, it return error as ->
Error: Route.get() requires a callback function but got a [object Undefined]
But when i replace next variable as follow from app.js:
var next = require('./routes/index');
app.get('/', next);
It work. So where do i need to change?
javascript node.js express
javascript node.js express
asked Nov 13 '18 at 12:11
Ghesa BhaktiGhesa Bhakti
31
31
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25
add a comment |
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25
add a comment |
2 Answers
2
active
oldest
votes
Simple..
In Node.js, when your use require, you aim to load a module
If you print (with console.log
) the result of require('./routes')
you will see that this module is not exposing index
whatsoever
When you use require('./routes/index')
you are loading a different module which contains the functionality you need
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
add a comment |
This code causes an error because when you use require it will automatically require the index.js file of the directory. So these 2 lines of code are the same.
var next = require('./routes');
var next = require('./routes/index');
So when you do this code it will cause an error because next.index does not exist.
var next = require('./routes');
app.get('/', next.index); // This will cause an error
The proper way to do this is:
var next = require('./routes');
app.get('/', next);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280761%2fi-want-to-do-basic-express-routing-but-it-return-error-route-get-requires%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple..
In Node.js, when your use require, you aim to load a module
If you print (with console.log
) the result of require('./routes')
you will see that this module is not exposing index
whatsoever
When you use require('./routes/index')
you are loading a different module which contains the functionality you need
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
add a comment |
Simple..
In Node.js, when your use require, you aim to load a module
If you print (with console.log
) the result of require('./routes')
you will see that this module is not exposing index
whatsoever
When you use require('./routes/index')
you are loading a different module which contains the functionality you need
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
add a comment |
Simple..
In Node.js, when your use require, you aim to load a module
If you print (with console.log
) the result of require('./routes')
you will see that this module is not exposing index
whatsoever
When you use require('./routes/index')
you are loading a different module which contains the functionality you need
Simple..
In Node.js, when your use require, you aim to load a module
If you print (with console.log
) the result of require('./routes')
you will see that this module is not exposing index
whatsoever
When you use require('./routes/index')
you are loading a different module which contains the functionality you need
answered Nov 13 '18 at 12:23
ymzymz
3,0551921
3,0551921
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
add a comment |
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
1
1
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
yeah he has the correct implementation at the bottom im just confused why hes asking how to get rid of the error when he has worked out what fixes it
– Joe Warner
Nov 13 '18 at 12:25
1
1
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
I think that he is trying to understand better what he is doing BEFORE it goes wrong.... I admire this kind of people that ask those questions... although it has NOTHING to do with express
– ymz
Nov 13 '18 at 12:27
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
oh no 100% should ask these questions to better understand whats going on just confused, i believe he understands the error message as he's worked out how to resolve, i guess its just a misunderstanding on how imports work
– Joe Warner
Nov 13 '18 at 12:30
add a comment |
This code causes an error because when you use require it will automatically require the index.js file of the directory. So these 2 lines of code are the same.
var next = require('./routes');
var next = require('./routes/index');
So when you do this code it will cause an error because next.index does not exist.
var next = require('./routes');
app.get('/', next.index); // This will cause an error
The proper way to do this is:
var next = require('./routes');
app.get('/', next);
add a comment |
This code causes an error because when you use require it will automatically require the index.js file of the directory. So these 2 lines of code are the same.
var next = require('./routes');
var next = require('./routes/index');
So when you do this code it will cause an error because next.index does not exist.
var next = require('./routes');
app.get('/', next.index); // This will cause an error
The proper way to do this is:
var next = require('./routes');
app.get('/', next);
add a comment |
This code causes an error because when you use require it will automatically require the index.js file of the directory. So these 2 lines of code are the same.
var next = require('./routes');
var next = require('./routes/index');
So when you do this code it will cause an error because next.index does not exist.
var next = require('./routes');
app.get('/', next.index); // This will cause an error
The proper way to do this is:
var next = require('./routes');
app.get('/', next);
This code causes an error because when you use require it will automatically require the index.js file of the directory. So these 2 lines of code are the same.
var next = require('./routes');
var next = require('./routes/index');
So when you do this code it will cause an error because next.index does not exist.
var next = require('./routes');
app.get('/', next.index); // This will cause an error
The proper way to do this is:
var next = require('./routes');
app.get('/', next);
answered Nov 13 '18 at 17:38
Mitchell HuxholdMitchell Huxhold
14
14
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280761%2fi-want-to-do-basic-express-routing-but-it-return-error-route-get-requires%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what do you mean where do i need to change?
– Joe Warner
Nov 13 '18 at 12:15
I mean how to get rid of error -> Error: Route.get() requires a callback function but got a [object Undefined] when i used the code in app.js (in the first code)
– Ghesa Bhakti
Nov 13 '18 at 12:19
the first code is wrong there is no index property in routes the second is correct so i dont understand why you're trying to do the first one
– Joe Warner
Nov 13 '18 at 12:25