GET request from Express js

GET Request from Express js ...

Express helper functions

Express has some helper functions for each one of the HTTP verbs and they are easy to remember because each function has the same name as the verb.

When we are browsing to localhost:4000, the browser sends a GET request for the root document at this URL: '/'.
We can tell our server how to handle that request with code similar to this:


app.get('/groceries',(req,res)=>{
    res.send([
        {
            item:'milk',
            quantity:'2'
        },
        {
            item:'milk',
            quantity:'2'
        },
        {
            item:'milk',
            quantity:'2'
        },
    ])
})

The first argument to get() is the path that the server must handle, in our case the string '/'. The second argument is a function that takes two arguments. Req and res always go together The first argument contains information about the request that Express received. The second argument is an object that contains information we want Express to send back, so we can control what the client receives.


We name the arguments in the function passed to get() as request and response, but they could be called anything.
It's common to call them req and res for ease of typing, so that's how I will call them going forward.