TypeScript

Creating a Simple Blogging Platform with TypeScript


Introduction

In this guide, we will create a basic blogging platform using TypeScript. While this example is simplified and lacks the advanced features of a real-world platform, it provides a starting point for understanding how to structure and build a web application.

Features

Our simple blogging platform will include the following features:

  • Post Creation: Users can create new blog posts with titles and content.
  • Post Listing: A list of all blog posts is displayed on the main page.
  • Post Viewing: Users can click on a post to view its details.

Setting Up the Project

To create our blogging platform, follow these steps:

1. Create a Project Folder

Create a folder for your project and open it in your code editor. You can name the folder as per your preference.

2. Initialize a Node.js Project

Open your terminal or command prompt, navigate to your project folder, and run the following command to create a package.json file for your project:

npm init -y

3. Install TypeScript and Express

Install TypeScript and Express, which will be the foundation of our blogging platform:

npm install typescript express --save-dev

4. Configure TypeScript

Create a tsconfig.json file in your project folder and configure it for TypeScript:

{
  `compilerOptions`: {
    `target`: `ES6`,
    `module`: `CommonJS`,
    `outDir`: `./dist`
  },
  `include`: [
    `./src/**/*.ts`
  ]
}

Creating the Blogging Platform

We'll structure our blogging platform with three main components: routes, controllers, and views. Here's a simplified code example:

1. Create TypeScript Files

Create the following TypeScript files in your project's src folder:

app.ts:

import express from 'express';
import { blogRouter } from './routes/blog';
const app = express();
const port = 3000;
app.use(express.json());
app.use('/blog', blogRouter);
app.get('/', (req, res) => {
  res.send('Welcome to our Blogging Platform');
});
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
    

routes/blog.ts:

import express from 'express';
import { getAllPosts, getPost } from '../controllers/blog';
export const blogRouter = express.Router();
blogRouter.get('/', getAllPosts);
blogRouter.get('/:id', getPost);
    

controllers/blog.ts:

import { Request, Response } from 'express';
const posts = [
  { id: 1, title: 'First Post', content: 'This is the content of the first post.' },
  { id: 2, title: 'Second Post', content: 'This is the content of the second post.' },
];
export const getAllPosts = (req: Request, res: Response) => {
  res.json(posts);
};
export const getPost = (req: Request, res: Response) => {
  const postId = parseInt(req.params.id, 10);
  const post = posts.find((p) => p.id === postId);
  if (!post) {
    res.status(404).json({ error: 'Post not found' });
  } else {
    res.json(post);
  }
};
    

2. Run the Application

Open your terminal and run the TypeScript compiler to transpile your TypeScript code:

tsc

Run the application:

node ./dist/app.js

Your blogging platform is now running. You can access it by visiting http://localhost:3000 in your web browser.

Conclusion

Creating a fully-featured blogging platform is a significant undertaking, but this simplified example provides a foundation for building more complex web applications. As you explore TypeScript and Express, you can expand and enhance your blogging platform with additional features, database integration, user authentication, and more.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.