Function isn't being called in Node Js using REST API












2















I am writing a code in Node JS, which uses mustache for templating html and REST API as backend.



Here is my code that doesn't work.



function setupRoutes(app) {
const base = app.locals.base;

app.get(`${base}/search.html`,doSearchContent(app));
app.get(`${base}/:name`,doGetContent(app));
}

function doSearchContent(app) {
return async function(req, res) {
console.log("here");
}; };


When I run my program and go to base/search.html. It never calls the doSearchContent method.



Any idea why and how I can fix this?



EDIT: The doGetContent works as expected. It's when I run the search.html it doesn't










share|improve this question

























  • Can you share what framework you are using? You're using some kind of router.

    – Evert
    Nov 18 '18 at 18:49
















2















I am writing a code in Node JS, which uses mustache for templating html and REST API as backend.



Here is my code that doesn't work.



function setupRoutes(app) {
const base = app.locals.base;

app.get(`${base}/search.html`,doSearchContent(app));
app.get(`${base}/:name`,doGetContent(app));
}

function doSearchContent(app) {
return async function(req, res) {
console.log("here");
}; };


When I run my program and go to base/search.html. It never calls the doSearchContent method.



Any idea why and how I can fix this?



EDIT: The doGetContent works as expected. It's when I run the search.html it doesn't










share|improve this question

























  • Can you share what framework you are using? You're using some kind of router.

    – Evert
    Nov 18 '18 at 18:49














2












2








2








I am writing a code in Node JS, which uses mustache for templating html and REST API as backend.



Here is my code that doesn't work.



function setupRoutes(app) {
const base = app.locals.base;

app.get(`${base}/search.html`,doSearchContent(app));
app.get(`${base}/:name`,doGetContent(app));
}

function doSearchContent(app) {
return async function(req, res) {
console.log("here");
}; };


When I run my program and go to base/search.html. It never calls the doSearchContent method.



Any idea why and how I can fix this?



EDIT: The doGetContent works as expected. It's when I run the search.html it doesn't










share|improve this question
















I am writing a code in Node JS, which uses mustache for templating html and REST API as backend.



Here is my code that doesn't work.



function setupRoutes(app) {
const base = app.locals.base;

app.get(`${base}/search.html`,doSearchContent(app));
app.get(`${base}/:name`,doGetContent(app));
}

function doSearchContent(app) {
return async function(req, res) {
console.log("here");
}; };


When I run my program and go to base/search.html. It never calls the doSearchContent method.



Any idea why and how I can fix this?



EDIT: The doGetContent works as expected. It's when I run the search.html it doesn't







node.js rest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 15:55







Mike Ross

















asked Nov 18 '18 at 4:41









Mike RossMike Ross

112




112













  • Can you share what framework you are using? You're using some kind of router.

    – Evert
    Nov 18 '18 at 18:49



















  • Can you share what framework you are using? You're using some kind of router.

    – Evert
    Nov 18 '18 at 18:49

















Can you share what framework you are using? You're using some kind of router.

– Evert
Nov 18 '18 at 18:49





Can you share what framework you are using? You're using some kind of router.

– Evert
Nov 18 '18 at 18:49












3 Answers
3






active

oldest

votes


















0














The express paths should be started with a leading slash. Please change your routes addition to something like this:



...
app.get(`/${base}/search.html`,doSearchContent(app));
app.get(`/${base}/:name`,doGetContent(app));
...


Express matches the path of http request against the 'path' provided for all routes to decide which routes must be called. Since the http paths always start with a slash, your routes also must specify those to match.






share|improve this answer
























  • the thing is doGetContent works as expected.

    – Mike Ross
    Nov 18 '18 at 15:56



















0














These lines



app.get(`${base}/search.html`,doSearchContent(app));   
app.get(`${base}/:name`,doGetContent(app));


are not working as you expect. In Express routes we don't invoke functions directly. Instead, we either pass a name of a callback fucntion to invoke, that receives req and res params, or an anonymous callback. In your case it could be something like this:



app.get(`${base}/search.html`,(req, res) => {
console.log("It's alive!");
doSearchContent(app);
});
app.get(`${base}/:name`, (req, res) => {
doGetContent(app)
});



The express paths should be started with a leading slash.




This is not true






share|improve this answer
























  • the thing is doGetContent works as expectef

    – Mike Ross
    Nov 18 '18 at 15:56











  • I tried using this method. It's alive still isn't being printed

    – Mike Ross
    Nov 18 '18 at 16:01











  • In OP's example, doSearchContent returns a function so this should still work.

    – Evert
    Nov 18 '18 at 17:01











  • @Evert Ideally this should work, that's why I am not sure why it isn't working

    – Mike Ross
    Nov 18 '18 at 18:35











  • @Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

    – Anton Pastukhov
    Nov 18 '18 at 19:53





















0














Have you added search.html file or used a template to build the HTML?



Make sure the template is being called and not the html file.



Other than that your code looks fine and it should work






share|improve this answer



















  • 1





    I had added an html file. Thanks. I made a template file and it works now :)

    – Mike Ross
    Nov 19 '18 at 23:54











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357965%2ffunction-isnt-being-called-in-node-js-using-rest-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The express paths should be started with a leading slash. Please change your routes addition to something like this:



...
app.get(`/${base}/search.html`,doSearchContent(app));
app.get(`/${base}/:name`,doGetContent(app));
...


Express matches the path of http request against the 'path' provided for all routes to decide which routes must be called. Since the http paths always start with a slash, your routes also must specify those to match.






share|improve this answer
























  • the thing is doGetContent works as expected.

    – Mike Ross
    Nov 18 '18 at 15:56
















0














The express paths should be started with a leading slash. Please change your routes addition to something like this:



...
app.get(`/${base}/search.html`,doSearchContent(app));
app.get(`/${base}/:name`,doGetContent(app));
...


Express matches the path of http request against the 'path' provided for all routes to decide which routes must be called. Since the http paths always start with a slash, your routes also must specify those to match.






share|improve this answer
























  • the thing is doGetContent works as expected.

    – Mike Ross
    Nov 18 '18 at 15:56














0












0








0







The express paths should be started with a leading slash. Please change your routes addition to something like this:



...
app.get(`/${base}/search.html`,doSearchContent(app));
app.get(`/${base}/:name`,doGetContent(app));
...


Express matches the path of http request against the 'path' provided for all routes to decide which routes must be called. Since the http paths always start with a slash, your routes also must specify those to match.






share|improve this answer













The express paths should be started with a leading slash. Please change your routes addition to something like this:



...
app.get(`/${base}/search.html`,doSearchContent(app));
app.get(`/${base}/:name`,doGetContent(app));
...


Express matches the path of http request against the 'path' provided for all routes to decide which routes must be called. Since the http paths always start with a slash, your routes also must specify those to match.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 7:07









elem4thelem4th

55727




55727













  • the thing is doGetContent works as expected.

    – Mike Ross
    Nov 18 '18 at 15:56



















  • the thing is doGetContent works as expected.

    – Mike Ross
    Nov 18 '18 at 15:56

















the thing is doGetContent works as expected.

– Mike Ross
Nov 18 '18 at 15:56





the thing is doGetContent works as expected.

– Mike Ross
Nov 18 '18 at 15:56













0














These lines



app.get(`${base}/search.html`,doSearchContent(app));   
app.get(`${base}/:name`,doGetContent(app));


are not working as you expect. In Express routes we don't invoke functions directly. Instead, we either pass a name of a callback fucntion to invoke, that receives req and res params, or an anonymous callback. In your case it could be something like this:



app.get(`${base}/search.html`,(req, res) => {
console.log("It's alive!");
doSearchContent(app);
});
app.get(`${base}/:name`, (req, res) => {
doGetContent(app)
});



The express paths should be started with a leading slash.




This is not true






share|improve this answer
























  • the thing is doGetContent works as expectef

    – Mike Ross
    Nov 18 '18 at 15:56











  • I tried using this method. It's alive still isn't being printed

    – Mike Ross
    Nov 18 '18 at 16:01











  • In OP's example, doSearchContent returns a function so this should still work.

    – Evert
    Nov 18 '18 at 17:01











  • @Evert Ideally this should work, that's why I am not sure why it isn't working

    – Mike Ross
    Nov 18 '18 at 18:35











  • @Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

    – Anton Pastukhov
    Nov 18 '18 at 19:53


















0














These lines



app.get(`${base}/search.html`,doSearchContent(app));   
app.get(`${base}/:name`,doGetContent(app));


are not working as you expect. In Express routes we don't invoke functions directly. Instead, we either pass a name of a callback fucntion to invoke, that receives req and res params, or an anonymous callback. In your case it could be something like this:



app.get(`${base}/search.html`,(req, res) => {
console.log("It's alive!");
doSearchContent(app);
});
app.get(`${base}/:name`, (req, res) => {
doGetContent(app)
});



The express paths should be started with a leading slash.




This is not true






share|improve this answer
























  • the thing is doGetContent works as expectef

    – Mike Ross
    Nov 18 '18 at 15:56











  • I tried using this method. It's alive still isn't being printed

    – Mike Ross
    Nov 18 '18 at 16:01











  • In OP's example, doSearchContent returns a function so this should still work.

    – Evert
    Nov 18 '18 at 17:01











  • @Evert Ideally this should work, that's why I am not sure why it isn't working

    – Mike Ross
    Nov 18 '18 at 18:35











  • @Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

    – Anton Pastukhov
    Nov 18 '18 at 19:53
















0












0








0







These lines



app.get(`${base}/search.html`,doSearchContent(app));   
app.get(`${base}/:name`,doGetContent(app));


are not working as you expect. In Express routes we don't invoke functions directly. Instead, we either pass a name of a callback fucntion to invoke, that receives req and res params, or an anonymous callback. In your case it could be something like this:



app.get(`${base}/search.html`,(req, res) => {
console.log("It's alive!");
doSearchContent(app);
});
app.get(`${base}/:name`, (req, res) => {
doGetContent(app)
});



The express paths should be started with a leading slash.




This is not true






share|improve this answer













These lines



app.get(`${base}/search.html`,doSearchContent(app));   
app.get(`${base}/:name`,doGetContent(app));


are not working as you expect. In Express routes we don't invoke functions directly. Instead, we either pass a name of a callback fucntion to invoke, that receives req and res params, or an anonymous callback. In your case it could be something like this:



app.get(`${base}/search.html`,(req, res) => {
console.log("It's alive!");
doSearchContent(app);
});
app.get(`${base}/:name`, (req, res) => {
doGetContent(app)
});



The express paths should be started with a leading slash.




This is not true







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 10:09









Anton PastukhovAnton Pastukhov

1978




1978













  • the thing is doGetContent works as expectef

    – Mike Ross
    Nov 18 '18 at 15:56











  • I tried using this method. It's alive still isn't being printed

    – Mike Ross
    Nov 18 '18 at 16:01











  • In OP's example, doSearchContent returns a function so this should still work.

    – Evert
    Nov 18 '18 at 17:01











  • @Evert Ideally this should work, that's why I am not sure why it isn't working

    – Mike Ross
    Nov 18 '18 at 18:35











  • @Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

    – Anton Pastukhov
    Nov 18 '18 at 19:53





















  • the thing is doGetContent works as expectef

    – Mike Ross
    Nov 18 '18 at 15:56











  • I tried using this method. It's alive still isn't being printed

    – Mike Ross
    Nov 18 '18 at 16:01











  • In OP's example, doSearchContent returns a function so this should still work.

    – Evert
    Nov 18 '18 at 17:01











  • @Evert Ideally this should work, that's why I am not sure why it isn't working

    – Mike Ross
    Nov 18 '18 at 18:35











  • @Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

    – Anton Pastukhov
    Nov 18 '18 at 19:53



















the thing is doGetContent works as expectef

– Mike Ross
Nov 18 '18 at 15:56





the thing is doGetContent works as expectef

– Mike Ross
Nov 18 '18 at 15:56













I tried using this method. It's alive still isn't being printed

– Mike Ross
Nov 18 '18 at 16:01





I tried using this method. It's alive still isn't being printed

– Mike Ross
Nov 18 '18 at 16:01













In OP's example, doSearchContent returns a function so this should still work.

– Evert
Nov 18 '18 at 17:01





In OP's example, doSearchContent returns a function so this should still work.

– Evert
Nov 18 '18 at 17:01













@Evert Ideally this should work, that's why I am not sure why it isn't working

– Mike Ross
Nov 18 '18 at 18:35





@Evert Ideally this should work, that's why I am not sure why it isn't working

– Mike Ross
Nov 18 '18 at 18:35













@Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

– Anton Pastukhov
Nov 18 '18 at 19:53







@Evert yeah, this is indeed an overlook. @Mike Ross in this case you could prepend your routes with app.use('*', (req, res, next) => { console.log(req.originalUrl); next() }) to check if you actual route matchs ${base}/search.html

– Anton Pastukhov
Nov 18 '18 at 19:53













0














Have you added search.html file or used a template to build the HTML?



Make sure the template is being called and not the html file.



Other than that your code looks fine and it should work






share|improve this answer



















  • 1





    I had added an html file. Thanks. I made a template file and it works now :)

    – Mike Ross
    Nov 19 '18 at 23:54
















0














Have you added search.html file or used a template to build the HTML?



Make sure the template is being called and not the html file.



Other than that your code looks fine and it should work






share|improve this answer



















  • 1





    I had added an html file. Thanks. I made a template file and it works now :)

    – Mike Ross
    Nov 19 '18 at 23:54














0












0








0







Have you added search.html file or used a template to build the HTML?



Make sure the template is being called and not the html file.



Other than that your code looks fine and it should work






share|improve this answer













Have you added search.html file or used a template to build the HTML?



Make sure the template is being called and not the html file.



Other than that your code looks fine and it should work







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 23:50









Dipesh DesaiDipesh Desai

409




409








  • 1





    I had added an html file. Thanks. I made a template file and it works now :)

    – Mike Ross
    Nov 19 '18 at 23:54














  • 1





    I had added an html file. Thanks. I made a template file and it works now :)

    – Mike Ross
    Nov 19 '18 at 23:54








1




1





I had added an html file. Thanks. I made a template file and it works now :)

– Mike Ross
Nov 19 '18 at 23:54





I had added an html file. Thanks. I made a template file and it works now :)

– Mike Ross
Nov 19 '18 at 23:54


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357965%2ffunction-isnt-being-called-in-node-js-using-rest-api%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents