Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Home Route

Harshay Raipancholi
seal-mask
.a{fill-rule:evenodd;}techdegree
Harshay Raipancholi
Python Development Techdegree Student 11,521 Points

Routes ; Help : function(request,Response)

Hi,

Can someone please explain to me - when do we actually pass the parameters request and response to this function? I'm confused, is 'response' the response coming back from the server?

2 Answers

Brian Anstett
Brian Anstett
5,831 Points

Express uses series of "Middleware function" to handle and manipulate requests to the server as they move through the "Request-Response Cycle." Middleware functions have three arguments (four if it's a custom error handling middleware function), request, response, and next. Note these are just parameters so you can name them whatever you want, this or their short hand req, res, next is what you'll see most often. req represents the request object TO THE SERVER. (https://expressjs.com/en/4x/api.html#req). res represents the HTTP response send BACK TO THE CLIENT (https://expressjs.com/en/4x/api.html#res). next is used to denote this middleware function has completed and to move on to the next middleware function. The idea of middleware for Express can be confusing but whether you knew it or not I'm sure you've already used it!

Let's take this example,

var express = require('express');
var app = express();

app.get('/heyshay', function(req, res){
  res.send('hello world');
});

app.listen(3000);

In the above example, we tell Express to route all requests to the path '/heyshay' to this middleware function. We could access properties about the request, such as query string or path parameters from req.query.name and req.param.name. We can them set properties for the response back the client such as a status code or a response string with res.status(200).send("HEY SHAY")

For more information checkout the Express docs, they're really really good docs. https://expressjs.com/en/4x/api.html

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

That is correct. The route handler (which is the type of method you are asking about) get called when the route is accessed by the client. The request from the client and the response that will be returned to the client are then passed in the the route handler. Once the response is closed, it is sent to the client. Hope that makes sense!