পৃষ্ঠাসমূহ

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 - Routing

Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. Koa does not support routes in the core module. We need to use the koa-router module to easily create routes in koa. Install this module using:

npm install --save koa-router
Now that we have koa-router installed, lets look at a simple GET route example:
var koa = require('koa');
var router = require('koa-router');
var app = koa();

var _ = router(); //Instantiate the router

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

function *getMessage(){
 this.body = "Hello world!";
};

app.use(_.routes()); //Use the routes defined using the router

app.listen(3000);
If we run our application and go to localhost:3000/hello, the server receives a get request at route "/hello", our koa app executes the callback function attached to this route and sends "Hello World!" as the response.
Hello We can also have multiple different methods at the same route. For example,
var koa = require('koa');
var router = require('koa-router');
var app = koa();

var _ = router(); //Instantiate the router

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

function *getMessage(){
 this.body = "Hello world!";
};

function *postMessage(){
 this.body = "You just called the post method at '/hello'!\n";
};

app.use(_.routes()); //Use the routes defined using the router

app.listen(3000);
To test this request, open up your terminal and use cURL to execute the following request
curl -X POST "https://localhost:3000/hello"
Curl request A special method, all, is provided by express to handle all types of http methods at a particular route using the same function. To use this method, try the following:
_.all('/test', allMessage);

function *allMessage(){
 this.body = "All HTTP calls regardless of the verb will get this response";
};

No comments:

Post a Comment