Design / Web/Posts Platform API
29 / 67

05/2023Design / Web

Posts Platform API

The REST backend for the Posts Platform — a single-resource CRUD service over posts, built on TypeScript, Express, and Mongoose, deployed on Vercel.

╌╌╌╌

The backend that serves the Posts Platform: a small REST service that stores posts and hands them back to the front-end. Built with TypeScript, Express, and Node, with posts persisted in MongoDB through Mongoose, and deployed on Vercel.

Every request follows one path: the router mounted at /api delegates to the post controller, which runs the Mongoose Post model against MongoDB and returns JSON.

The service does CRUD over one resource, a single Mongoose model, Post (models/post_model.ts): a title, a tags array of strings, content, and a coverUrl. The router in router.ts, mounted at /api, maps HTTP verbs onto it: POST /posts creates, GET /posts lists, GET /posts/:id fetches one, PUT /posts/:id edits, and DELETE /posts/:id removes. Each route stays thin, reading the body or the :id, calling the matching function in post_controller.ts (createPost, getPosts, getPost, updatePost, deletePost), and returning JSON.

A single PostType interface carries the types across the boundary, declaring a post's shape once and reusing it for the controller signatures and the request and response bodies, so a renamed field surfaces as a compile error rather than a runtime surprise. One wrinkle rides on it: a post stores tags as an array, and reformatPostTags in utils joins them into a comma string on the way out, so list and single-post responses both hand the front-end the form it expects.

The API stands alone by design. server.ts wires up cors, morgan request logging, and JSON body parsing, then connects to MONGODB_URI and mounts the router; api/index.ts re-exports the app for Vercel's serverless runtime. Keeping the API separate lets the Posts Platform front-end stay a pure client while storage and validation live behind a stable set of routes.

References

  1. Project repository
  2. Live site

╌╌ END ╌╌