Skip to content

Commit

Permalink
example: add view and render example
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mdy-m committed Aug 21, 2024
1 parent dd62794 commit 8dcaebb
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 38 deletions.
39 changes: 36 additions & 3 deletions examples/CRUD/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import { g } from '../../lib/core/server';
g.load('router');
g.init();
import { WebServer } from '../../lib/core/server';
import { Context } from '../../lib/types/types';
import ejs from 'ejs';
const app = new WebServer();

// Configure the template engine
app.engine('.ejs', async (path: string, options: object, callback: (err: Error | null, rendered?: string) => void) => {
ejs.renderFile(path, options, callback);
});

// Set default view engine and views directory
app.set('view engine', '.ejs');
app.set('views', './views');

app.use(async (ctx: Context, nxt: Function) => {
console.log('hello from mid');
await nxt();
});
// // Define routes
app.use('/api', async (ctx: any, next: () => Promise<void>) => {
// Example middleware for API routes
console.log('API route accessed');
await next();
});

// Define a route with `all` method
app.all('/about', async (ctx: Context) => {
ctx.render('index');
});

// Start the server
app.init({
port: 3000,
host: 'localhost',
logger: true,
});
209 changes: 175 additions & 34 deletions examples/CRUD/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/CRUD/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
},
"author": "",
"license": "ISC",
"description": ""
"devDependencies": {
"@types/ejs": "^3.1.5",
"ejs": "^3.1.10"
}
}
9 changes: 9 additions & 0 deletions examples/CRUD/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>hello brother</body>
</html>

0 comments on commit 8dcaebb

Please sign in to comment.