Docs
Guide
Define Handler

Define Handler

To handle requests in a route file, you must follow the dobs handler form.

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter((req, res) => {
  res.send("I'm here!");
});

This responds with I'm here! to all requests, regardless of the HTTP method.

TIP

The defineRouter() function exists solely for type safety. It can be used in JavaScript, but it’s optional in TypeScript.

#Handling Different Methods

To handle requests differently for each HTTP method, export an object that includes method-specific handlers.

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter({
  GET(req, res) {
    res.send({ message: "This is GET" });
  },
  POST(req, res) {
    res.send({ message: "This is POST" });
  },
});

The handler in this example is written in function form. If you want to return constant values instead, you can write it like this:

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter({
  GET: { message: "This is GET" },
  POST: { message: "This is POST" },
});

WARNING

Constant handler data must be of the object type.

#Handling All Requests

If you want to handle all requests regardless of method, define an ALL handler.

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter((req, res) => {});

Or:

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter({
  ALL(req, res) {},
});

If an ALL handler is included in the exported object, all requests will be processed by it, regardless of other handlers.

#Define 404 Handler

404 핸들러는 configuration file에서 설정할 수 있습니다.

Typescript
import { defineConfig } from "dobs";
 
export default defineConfig({
  onNotFound: (req, res, next) => {
    res.status(404).end();
  },
});