LogoRobo.js

Quick start

Create your first API route and start serving requests in minutes.

Get a web server running in your Robo project with just a few files.

You need an existing Robo project. If you don't have one, run npx create-robo my-project -p @robojs/server.

Setup

Install the plugin

npx robo add @robojs/server@next

Create an API route

hello.tsGET /api/hello
src/api/hello.ts
				export function GET() {
					return { message: 'Hello, world.' }
				}
src/api/hello.js
				export function GET() {
					return { message: 'Hello, world.' }
				}

Start your Robo

npx robo dev

Visit http://localhost:3000/api/hello to see the response.

POST requests

Export a POST function to handle POST requests at the same path. Use request.json() to parse the request body.

src/api/users.ts
import type { RoboRequest } from '@robojs/server'

export function GET() {
	return [{ id: '1', name: 'Alice' }]
}

export async function POST(request: RoboRequest) {
	const body = await request.json()
	return { created: true, user: body }
}
src/api/users.js

export function GET() {
	return [{ id: '1', name: 'Alice' }]
}

export async function POST(request) {
	const body = await request.json()
	return { created: true, user: body }
}

Dynamic parameters

Wrap a filename segment in brackets to create a dynamic route parameter. Access it via request.params.

[id].tsGET /api/users/:id
src/api/users/[id].ts
import type { RoboRequest } from '@robojs/server'

export function GET(request: RoboRequest) {
	return { id: request.params.id }
}
src/api/users/[id].js

export function GET(request) {
	return { id: request.params.id }
}

Visit /api/users/123 to see { "id": "123" }.

Configuration

Server settings live in a plugin config file. Here's a minimal example that changes the port and enables CORS:

config/plugins/robojs/server.mjs
export default {
	port: 5000,
	cors: true
}
config/plugins/robojs/server.mjs
export default {
	port: 5000,
	cors: true
}

Next steps

On this page