0

I am new to express node and I am responsible for the front end of a web app. I need to be able to have the user press a link and for that link to when press pass variable to index.js this link lets the user choose what category of question that they want to be tested on. I do not think that having a different route for each category is the best option. but because I am new I am not sure what to do.

Here is the code for my link in jade (I want to add something here to do what I want) there is a link to a screenshot of the page at the bottom of this post.

              li.active
                 a(href='freeplay')
                    i.fa.fa-edit
                    span.nav-label Freeplay
                    span.fa.arrow
                 ul.nav.nav-second-level
                    li
                       a(href='freeplay/category') Category1
                    li
                       a(href='freeplay/category') Category2
                    li
                       a(href='freeplay/category') Category3
                    li
                       a(href='freeplay/category') Category4
                    li
                       a(href='freeplay/category') Category5

and Here is my index.js that handles it. temp is the variable that I want to hold the string of the category.

//Handle the free play request
router.get('/freeplay' , isAuthenticated, freeplay.startFreePlay);

//Handle the free play request with category
router.get('/freeplay/category' , isAuthenticated, freeplay.startCategoryPlay);

and finally the node.js that I want to be able to read a variable in to temp is the variable I want to assign the user chosen category to.

exports.startFreePlay = function(req, res) {

//grab a question that the user has not gotten right
//Get the users logged in id
userId = req.session.passport.user;


freePlayLogic.getQuestion(userId, function(question){


    res.render("FreePlayQuestion", { question: question , questionID : question._id.toString()});
})

//Pass the question to render

};

exports.startCategoryPlay = function(req, res){
//grab a question that the user has not gotten right
//Get the users logged in id
userId = req.session.passport.user;

/**
 * get a question from category "temp"
 */
freePlayLogic.getQuestionFromCategory(userId, "temp", function(question){

    res.render("FreePlayQuestion", { question: question , questionID :   question._id.toString()});
})
}

Thanks in advance for help!

here is a screenshot of the web app with the category choices

4
  • I don't think you really understand what jade is doing. Jade is just a template language. It isn't a front-end framework. You cannot pass a variable from jade to your node server. You have to have client side logic, like javascript(browser side) in order to send back a value to nodejs. Commented Dec 11, 2015 at 7:01
  • ok I would love to learn. What can/should I look into doing? My background is mostly in c and assembly and I have just recently learned java this semester in my engineering class. This is the first time I have dealt with javascript. Commented Dec 11, 2015 at 7:10
  • I guess I don't know what you're trying to do. It wouldn't hurt to do a couple of tutorials on MEAN stack so you can understand how to manage state between server and client, send data back and forth and understanding whats server logic and whats client logic. Commented Dec 11, 2015 at 7:28
  • Thank you for the help @tsturzl like I said I am new at this kind of development and I guess I just need to spend some more time learning about the MEAN stack. My problem is just that after coming from such low level languages I have a hard time using code that I don't understand how it works inside and out. (I am a geek and I like to look into how API's implement the functions) Commented Dec 11, 2015 at 7:36

1 Answer 1

1

so what it looks like you are trying to do is have a button send data back to your server. Doing this requires making another request to the server. You can add a route which will process this data like so...

app.get('/question/:id', function(req, res) {
    var id = req.params.id;
    //do something with this id like render a page
);

And then your links would look something like this

a(href="/question/1") Question 1
a(href="/question/2") Question 2

This should work, if you have any questions just leave a comment :)

Try adding this test route

app.get('/test/id', function(req, res) {
    res.send(req.params.id);
);
Sign up to request clarification or add additional context in comments.

5 Comments

I guess to me it feels like there should be a better way of doing it then making a new route for each button press
This only add one route, which works for all button presses
oh... so then I could assign a string to a variable based on the number using some sort of if statement? or can I pass a string directly....Thanks so much for answering
I added a test route, add that as it is simpler
Thank you, I will try to implement your solution tomorrow. It is getting late here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.