forked from kvnp/youtune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
34 lines (29 loc) · 917 Bytes
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Application } from 'https://deno.land/x/[email protected]/mod.ts';
const app = new Application();
// Send static content
app.use(async (ctx, next) => {
try {
await ctx.send({
root: `${Deno.cwd()}/`,
index: 'index.js',
});
} catch {
await next();
}
});
// 404 handler (redirect to index.js)
app.use((ctx) => {
// handle index.js not found
if (
ctx.request.url.pathname === '/' ||
ctx.request.url.pathname ==='/ index.js'
) {
ctx.response.status = 404;
ctx.response.body = 'Not found.\nSeems index.html is missing.';
return;
}
ctx.response.status = 301;
ctx.response.redirect('/');
});
console.log('App listening on port', 3000);
await app.listen({ port: 3000 });