Connect one router to another router within the Oak framework, similar to how it is done in

I have a goal of setting up multiple routers along with a main router that can route requests to the other routers.

router.use("/strategy", strategyRoutes);
router.use("/account", accountRoutes);

The objects router, strategyRoutes, and accountRoutes are instances of express.Router(). While I can achieve this in express, I am curious if there is a way to accomplish the same functionality in Deno's Oak Framework. The router in Oak does have a router.use function, but it only accepts a middleware function and not another router.

Answer №2

Upon further investigation, I have identified a workaround for the issue at hand. The motivation behind wanting this feature was to consolidate the routing logic within separate modules. Here is the solution I came up with:

// router/index.ts

import { Router } from "https://deno.land/x/oak/mod.ts";
import withAccountRoutes from "./account.ts";
import withContractRoutes from "./contract.ts";
const router = new Router();
withAccountRoutes(router);
withContractRoutes(router);
export default router;

// router/account.ts

import { Router } from "https://deno.land/x/oak/mod.ts";

const SUB_ROUTE = "/account";

const withAccountRoutes = (router: Router) => {
  router
    .get(SUB_ROUTE + "/new", ({ request, response, cookies }) => {
      console.log("Cookies : ", cookies);
      response.body = "Coming soon";
    })
    .get(SUB_ROUTE + "/current", ({ request, response, cookies }) => {
      response.body = "You are the wise one";
    });
};

export default withAccountRoutes;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Formik button starts off with enabled state at the beginning

My current setup involves using Formik validation to disable a button if the validation schema is not met, specifically for a phone number input where typing alphabets results in the button being disabled. However, I encountered an issue where initially, ...

The 'Group' type is lacking the 'children' properties needed for the 'Element' type in Kendo UI Charts Drawing when using e.createVisual()

In my Angular 10 project, I utilized the following function to draw Kendo Charts on Donut Chart public visual(e: SeriesVisualArgs): Group { // Obtain parameters for the segments this.center = e.center; this.radius = e.innerRadius; // Crea ...

Why is it possible for the EXPRESS+EJS template to access CONFIG without explicitly passing it when rendering?

Currently exploring my knowledge of node.js alongside express and the ejs template. As I delved into some code, I stumbled upon the fact that they were able to invoke config in the template without explicitly passing it as a variable during rendering. You ...

Creating an object type that includes boolean values, ensuring that at least one of them is true

To ensure both AgeDivisions and EventStyles have at least one true value, I need to create a unique type for each. These are the types: type AgeDivisions = { youth: boolean; middleSchool: boolean; highSchool: boolean; college: boolean; open: bo ...

Angular: Refresh mat-table with updated data array after applying filter

I have implemented a filter function in my Angular project to display only specific data in a mat-table based on the filter criteria. Within my mat-table, I am providing an array of objects to populate the table. The filtering function I have created loo ...

Ways to execute a function both before and after every request within the Express framework

Utilizing a REST API in Node with Express, I have multiple endpoints set up. router.get('/admin/logs', (req, res) => { I am looking to execute a specific function when ALL endpoints are called. Is there a global way to achieve this without ...

Customizing number input types in Angular 2 - the perfect solution

Attempting to incorporate a time picker using HTML5 input type number in Angular2. The code snippet below illustrates the setup: <input type="number" [(ngModel)]="hour" (change)="checkHours();" name="one" min="1" max="12"> <input type="number" ...

Another component's Angular event emitter is causing confusion with that of a different component

INTRODUCTION In my project, I have created two components: image-input-single and a test container. The image-input-single component is a "dumb" component that simplifies the process of selecting an image, compressing it, and retrieving its URL. The Type ...

Typescript is unable to comprehend that the initial item in an array of strings is considered to be a string

Here are the functions I am working with: const transitionGroup = ( propertyName: string, durationMultiple = 1, timingFunction = 'linear', delayMultiple = 0, ): string => { // ...more logic here return [propertyName, duration, tim ...

"Using regular expressions in a MongoDB find() query does not provide the desired

app.get("/expenses/:month", async (req, res) => { const { month } = req.params; const regexp = new RegExp("\d\d\d\d-" + month + "-\d\d"); console.log(regexp); const allExpenses ...

Mapping a TypeScript tuple into a new tuple by leveraging Array.map

I attempted to transform a TypeScript tuple into another tuple using Array.map in the following manner: const tuple1: [number, number, number] = [1, 2, 3]; const tuple2: [number, number, number] = tuple1.map(x => x * 2); console.log(tuple2); TypeScript ...

Delete a targeted express session in connect-mongo manually

What steps can I take to remove a specific session from mongoDB? I am trying to figure out how to allow myself to delete a particular session from the database (belonging to another user). According to the connect-mongo documentation, I could use the dest ...

Setting up state using the useState hook in a Typescript environment

At some point in the future, the state will be updated using the useState hook with an array of objects. The interface for this array of objects will look like this: export interface DataSource { dataPiont: Array<DataPoint>; } export interface Dat ...

Encountering a DNS_PROBE_FINISHED_NXDOMAIN error when trying to use EC2 for Google OAuth

While utilizing an Ubuntu EC2 instance for my Google OAuth 2.0 strategy with the Google API platform, I keep encountering the DNS_PROBE_FINISHED_NXDOMAIN error. After choosing the desired Gmail account for authentication, the /google/callback is not funct ...

Obtaining parameter types for functions from deeply nested types

I'm currently facing a challenge involving deeply nested parameters. When dealing with non-nested parameters, everything functions smoothly without any issues export type test = { 'fnc1': () => void, 'fnc2': () => void, ...

Error in Ionic2 TypeScript: 'google' and '$' names not found, despite Google Maps and jQuery functioning correctly

I am currently working on developing an ionic2 application using TypeScript. Within the index.html file, I have attempted to integrate jquery and the Google Maps JS API before cordova.js: <!-- Vendor --> <script src="https://maps.googleapis. ...

Excessive geolocation position responses in Angular 5

I am trying to implement an Angular 5 component that will continuously fetch my current location every 3 seconds if it has changed. Here is a snippet of my code: export class WorkComponent implements OnInit { constructor(private userService: UserService ...

Node.js and Express: ensuring completion of asynchronous operation before responding to HTTP request

Imagine there is a callback function set up for an HTTP GET request, structured like this: router.get('/latestpost', function(req, res, next) { var data = new FbData(); get_latest_post (data); get_post_image (data); res.json(da ...

Finding a solution to the dilemma of which comes first, the chicken or the egg, when it comes to using `tsc

My folder structure consists of: dist/ src/ In the src directory, I have my .ts files and in dist, I keep my .js files. (My tsconfig.json file specifies "outDir":"dist" and includes 'src'). Please note that 'dist' is listed in my git ...

A more efficient method for querying documents based on ids that are not in a given list and then sorting them by a specific publish date

After implementing the code provided below, I noticed that the performance tests indicate each request takes a second or longer to complete. My goal is to enhance this speed by at least 10 times. The bottleneck seems to be caused by the NOT operator resu ...