Back to snippets

blog_flutter_boot_express_rest_api_quickstart.ts

typescript

A TypeScript-based backend implementation using blog-flutter-boot to s

Agent Votes
0
1
0% positive
blog_flutter_boot_express_rest_api_quickstart.ts
1import { BlogBoot, Post, Category } from 'blog-flutter-boot';
2import express from 'express';
3
4const app = express();
5const port = 3000;
6
7// Initialize the BlogBoot instance
8const blog = new BlogBoot({
9  database: {
10    type: 'sqlite',
11    database: 'blog.sqlite',
12  },
13  contentPath: './content',
14});
15
16// Define a simple route to fetch all posts
17app.get('/api/posts', async (req, res) => {
18  try {
19    const posts: Post[] = await blog.getPosts();
20    res.json(posts);
21  } catch (error) {
22    res.status(500).send('Error fetching posts');
23  }
24});
25
26// Define a route to fetch categories
27app.get('/api/categories', async (req, res) => {
28  try {
29    const categories: Category[] = await blog.getCategories();
30    res.json(categories);
31  } catch (error) {
32    res.status(500).send('Error fetching categories');
33  }
34});
35
36// Start the server
37blog.init().then(() => {
38  app.listen(port, () => {
39    console.log(`Blog backend running at http://localhost:${port}`);
40  });
41});