পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Thursday, March 2, 2017

Koa.js - Request Object

A Koa Request object is an abstraction on top of node's vanilla request object, providing additional functionality that is useful for every day HTTP server development. The Koa request object is embeded in the context object, this. Lets log out the request object whenever we get a request:

var koa = require('koa');
var router = require('koa-router');
var app = koa();

var _ = router();

_.get('/hello', getMessage);

function *getMessage(){
    console.log(this.request);
    this.body = 'Your request has been logged.';
}

app.use(_.routes());

app.listen(3000);
When you run this code and navigate to https://localhost:3000/hello then you'll get the response:
On your console, you'll get the request object logged out:
{ 
    method: 'GET',
    url: '/hello/',
    header: 
    { 
        host: 'localhost:3000',
        connection: 'keep-alive',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        dnt: '1',
        'accept-encoding': 'gzip, deflate, sdch',
        'accept-language': 'en-US,en;q=0.8' 
    }
}
We have access to many useful properties of the request using this object. Let us look at some examples:

request.header

Gives us all the request headers.

request.method

Gives us the request method(GET, POST, etc.)

request.href

Gives us the full request URL.

request.path

Gives us just the path of the request. Without query string and base url.

request.query

Gives us the parsed query string. For example if we log this on a request like https://localhost:3000/hello/?name=Ayush&age=20&country=India, then we'll get the following object:
{
    name: 'Ayush',
    age: '20',
    country: 'India'
}

request.accepts(type)

This function returns true or false based on whether the requested resources accepts the given request type.
You can read more about the request object in the docs at Request.

No comments:

Post a Comment