• Townie
    AI
  • Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
muhdsaeed1

muhdsaeed1

expressweb

Public
Like
expressweb
Home
Code
6
controllers
1
middleware
1
models
1
routes
1
README.md
H
index.ts
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
Code
/
Search
index.ts
https://muhdsaeed1--a98ceb443bad11f098ba9e149126039e.web.val.run
README.md

User API - Express/Mongoose Patterns Adapted for Val Town

This project demonstrates how to adapt familiar Express.js and Mongoose patterns to work in Val Town's Deno environment using Hono and SQLite.

Architecture

The project follows Express/Mongoose conventions but uses Val Town's stack:

  • Express → Hono: Web framework for routing and middleware
  • MongoDB/Mongoose → SQLite: Database with ORM-like patterns
  • Node.js → Deno: Runtime environment

Project Structure

├── index.ts                 # Main app (like Express app.js)
├── models/
│   └── User.ts             # User model (Mongoose-style patterns)
├── controllers/
│   └── userController.ts   # Route handlers (Express controller style)
├── routes/
│   └── userRoutes.ts       # Route definitions (Express router style)
├── middleware/
│   └── errorHandler.ts     # Error handling middleware
└── README.md

API Endpoints

Get User by ID

GET /api/users/:id

Response:

{ "success": true, "data": { "id": 1, "name": "John Doe", "email": "john@example.com", "createdAt": "2024-01-01T00:00:00.000Z", "updatedAt": "2024-01-01T00:00:00.000Z" } }

Get All Users

GET /api/users

Create User

POST /api/users
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com"
}

Update User

PUT /api/users/:id
Content-Type: application/json

{
  "name": "Jane Doe",
  "email": "jane@example.com"
}

Delete User

DELETE /api/users/:id

Key Adaptations

1. Model Layer (Mongoose → SQLite)

  • UserModel.findById() - Similar to Mongoose User.findById()
  • UserModel.create() - Similar to Mongoose User.create()
  • UserModel.findByIdAndUpdate() - Similar to Mongoose method
  • Static methods instead of instance methods

2. Controller Layer (Express Controllers)

  • Same controller pattern with error handling
  • JSON responses with consistent structure
  • HTTP status codes and error messages

3. Routing (Express Router → Hono Router)

  • Similar route definition syntax
  • Middleware support
  • Parameter extraction with c.req.param()

4. Middleware (Express Middleware → Hono Middleware)

  • Error handling middleware
  • Request logging (similar to Morgan)
  • Async middleware support

Usage Examples

Testing the API

  1. Get user by ID:
curl https://your-val.web.val.run/api/users/1
  1. Create a new user:
curl -X POST https://your-val.web.val.run/api/users \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "email": "john@example.com"}'
  1. Get all users:
curl https://your-val.web.val.run/api/users

Error Handling

The API includes comprehensive error handling:

  • 400: Bad Request (invalid parameters)
  • 404: Not Found (user doesn't exist)
  • 409: Conflict (email already exists)
  • 500: Internal Server Error

Development Notes

  • Database is automatically initialized on startup
  • SQLite table is created if it doesn't exist
  • All responses follow a consistent JSON structure
  • Proper TypeScript typing throughout
  • Error logging for debugging
HTTP
  • index.ts
    muhdsaeed1--a9…9e.web.val.run
Code
controllersmiddlewaremodelsroutesREADME.md
H
index.ts
FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.