Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Fredrik Blomberg
Fredrik Blomberg
13,012 Points

Communication backend and frontend.

I have taken parts of the full stack javascript track here at treehouse. Excellent courses! I kind of get the hang of express, mongodb and frontend javascript. However i find communication between my frontend scripts and backend quite hard.

Im trying to make some different quizzes that you can choose from a list. The list is loaded using express and the data comes from mongodb.

I want the user to be able to choose a quiz from the list and by clicking on the quiz the user will be redirected to the quizroute, and the correct quiz will be loaded. The code for the click event is in a separate javascript(jQuery) file. Thus information from the click event in the front-end file needs to be passed to express and the user redirected. Is there an easy way to do this? Is ajax part of the solution? Can i export the "choice" of quiz made in the javascript file to express? Unfortunately my code is getting a bit to long and complicated to post here.

Will be very greatfull for a simple solution! Thank you!

3 Answers

Brodey Newman
Brodey Newman
10,962 Points

If i'm understanding you correctly, you should just be able to serve your static files with express like below. The public folder will include the javascript files for your client like your main.js for example.

// serve static files from /public folder
app.use(express.static(__dirname + '/public'));

Then in your index.pug file you can import your script like below.

script(src='/public/main.js')

Hope this helps! I'm no pro though so If it doesn't then i'm sorry! Haha

Fredrik Blomberg
Fredrik Blomberg
13,012 Points

Thank you for your answer, I appreciate your effort! haha, its sometimes hard to formulate a good question. Especially when no code is included. I kind of get how to include my script file in jade and express. However i find it hard to exchange information between my scriptfile and express.

Brodey Newman
Brodey Newman
10,962 Points

So are you saying you you're trying to figure out how to send the answers the person selected in the quiz to express so then It can store the data in Mongo?

Fredrik Blomberg
Fredrik Blomberg
13,012 Points

I have a page where different quizzes are listed. The user can choose between these quizzes by clicking on them(Eg. "the world war 2 quiz", "the dr. Phil quiz", "The netflix series quiz" . When the user clicks on a quiz(Eg the dr Phil quiz) the correct quiz is loaded in the "/quiz" route. So the information that needs to be passed from my scriptfile to express is what quiz the user chooses. Express will then find the right quiz(dependent on what choice the user made) and load the questions which are stored in mongodb. I have kind of got it to work using ajax, but i figure there must be an easier way to pass information from a clickevent in my scriptfile to express.