Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
On this page

File-based routing

If you've used frameworks likeNext.js, you might befamiliar with file based routing - you add a file in a specific directory and itautomatically becomes a route. This tutorial demonstrates how to create a simpleHTTP server that uses file based routing.

Route requestsJump to heading

Create a new file calledserver.ts. This file will be used to route requests.Set up an async function calledhandler that takes a request object as anargument:

server.ts
asyncfunctionhandler(req: Request):Promise<Response>{const url=newURL(req.url);const path= url.pathname;const method= req.method;let module;try{    module=awaitimport(`.${path}.ts`);}catch(_error){returnnewResponse("Not found",{ status:404});}if(module[method]){return module[method](req);}returnnewResponse("Method not implemented",{ status:501});}Deno.serve(handler);

Thehandler function sets up a path variable which contains the path,extracted from the request URL, and a method variable which contains the requestmethod.

It then tries to import a module based on the path. If the module is not found,it returns a 404 response.

If the module is found, it checks if the module has a method handler for therequest method. If the method handler is found, it calls the method handler withthe request object. If the method handler is not found, it returns a 501response.

Finally, it serves the handler function usingDeno.serve.

The path could be any valid URL path such as/users,/posts, etc. Forpaths like/users, the file./users.ts will be imported. However, deeperpaths like/org/users will require a file./org/users.ts. You can createnested routes by creating nested directories and files.

Handle requestsJump to heading

Create a new file calledusers.ts in the same directory asserver.ts. Thisfile will be used to handle requests to the/users path. We'll use aGETrequest as an example. You could add more HTTP methods such asPOST,PUT,DELETE, etc.

Inusers.ts, set up an async function calledGET that takes a request objectas an argument:

users.ts
exportfunctionGET(_req: Request): Response{returnnewResponse("Hello from user.ts",{ status:200});}

Start the serverJump to heading

To start the server, run the following command:

deno run --allow-net --allow-read server.ts

This will start the server onlocalhost:8080. You can now make aGET requesttolocalhost:8000/users and you should see the responseHello from user.ts.

This command requires the--allow-net and--allow-readpermissions flags to allow access to thenetwork to start the server and to read theusers.ts file from the filesystem.

🦕 Now you can set up routing in your apps based on file structure. You canextend this example to add more routes and methods as needed.

Thanks to@naishe for contributing thistutorial.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp