Search code examples
javascriptexpresshttpgetsend

What’s the difference between res.send and app.post?


I’m new to express and HTTP. The express library has app.get, app.post and res.send. As I understand it, app.get uses/is used with GET and app.post POST. Does res.send call POST?


Solution

  • res.send() sends a response to an incoming http request that has come into your http server.

    app.post() registers a request handler with Express for a specific URL in your http server and for POST requests so that when your Express server receives a POST request at that URL, it will call this request handler.

    Here's an example of res.send():

    // configure request handler for GET request to /
    app.get("/", (req, res) => {
        res.send("hi");               // send response to incoming http request
    });
    

    Here's an example of app.post():

    // this middleware reads and parses the body for content-type
    // of  application/x-www-form-urlencoded
    app.use(express.urlencoded({extended: true}));
    
    // configure request handler for POST request to /login
    app.post("/login", (req, res) => {
         // in a real request handler, this would be some sort of username
         // and password comparison in a database and using appropriate crypto
         if (req.body.username === "John" && req.body.password === "foobar99") {
             res.send("login successful");
         } else {
             res.status(401).send("Login failed");
         }
    });
    

    The express library has res.get and res.send. As I understand it, res.get uses / is used with GET.

    You are perhaps confused because there is no res.get. Perhaps you meant app.get()? If so, app.get() configures a request handler for an http GET request to a specific URL. So, app.get("/books", ...) might display a page of all available books.

    Does res.send call POST?

    No. res.send() sends a response to an http request.

    Here are the steps you can think of.

    1. An http client (such as a browser or any piece of code) creates an http request and sends that request to a server.

    2. That request will have both an HTTP verb such as GET, POST, PATCH, etc... and it will have a URL such as /login or /books.

    3. The web server that the request was sent to receives that request. In the case of a web server using the Express framework, the Express server looks through its list of already registered routes (these routes were previously registered with app.get(...), app.post(...), app.use(...), etc... These are essentially listeners for specific routes. If the Express server finds an already registered route that matches both the http request verb and the URL, then it calls that route handler and pass it three arguments (req, res, next).

    4. When the request handler code gets called, it can examine the req object to see any data about the incoming request such as the exact URL, any http headers on the request and so on. It can use the res object for sending a response such as setting any http headers on the response, setting content-type, setting an http status code for the response, creating and sending the body of the response, etc... res.send() is one way to send the response. There are also others such as res.sendFile(), res.json(), res.sendStatus() and so on...

    5. After one of these methods is called that sends the response, then the underlying engine sends that http response back to the client that sent the original http request and the HTTP request/response is considered complete.