简体   繁体   English

如何处理 deno 中的 static 文件夹

[英]How to handle the static folder in deno

I would like to know if it's possible to change the static resource file depending on the URL.我想知道是否可以根据 URL 更改 static 资源文件。 For example, here is my folder pattern:例如,这是我的文件夹模式:

/root
-> server.ts
-> /project**s**
    -> some libraries used in index.html files
    -> /project1
       -> index.html
       -> some files used in the index.html
    -> /project2
       -> index.html
       -> some files used in the index.html

and this is how to handle static files in deno using the oak library:这是使用橡树库在 deno 中处理 static 文件的方法:

app.use(async (ctx, next) => {    
    await send(ctx, ctx.request.url.pathname, {
        root: `${Deno.cwd()}/static`,
    })

    next()
});

My goal is when you type in the URL: mydomain.com/project/project1 return you the index.html file corresponding to the project id.我的目标是当您输入 URL 时: mydomain.com/project/project1返回与项目 ID 对应的index.html文件。

For now, I'm using oak router to redirect URLs like that:现在,我使用 Oak 路由器来重定向 URL,如下所示:

const router = new Router();
router.get('/project/:project_id', project)
export const project = async (ctx: RouterContext) => {
    ctx.render(`${Deno.cwd()}/project**s**/` + ctx.params.projectid + '/index.html');
}

Thanks for your help.谢谢你的帮助。

Does this answer your question?这回答了你的问题了吗?

Example: Dynamically check for existing folders, if not found you'll get error: No such file or directory (os error 2)示例:动态检查现有文件夹,如果找不到,您将收到错误: No such file or directory (os error 2)

.
├── projects
│   └── foo
│       └── index.html
└── server.ts
// ./server.ts

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

const app = new Application();

app.use(async (context) => {
    await context.send({
        root: Deno.cwd(),
        index: "index.html",
    });
});

await app.listen({ port: 8000 });
// ./projects/foo/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>foo</h1>
</body>
</html>
deno run --allow-net --allow-read=. server.ts

# or /foo, /foo/index.html
curl localhost:8000/projects/foo/
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>foo</h1>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM