-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: add view and render example
- Loading branch information
Showing
4 changed files
with
224 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |