Hello World
Minimal NextRush application — your first API in under 10 lines.
The simplest possible NextRush application.
Prerequisites
Node.js 22+ and TypeScript 5.x are required.
The Code
import { createApp, createRouter, listen } from 'nextrush';
const app = createApp();
const router = createRouter();
router.get('/', (ctx) => {
ctx.json({ message: 'Hello, NextRush!' });
});
app.route('/', router);
await listen(app, 3000);Expected output:
curl http://localhost:3000
# {"message":"Hello, NextRush!"}Step by Step
Create Project
mkdir hello-nextrush && cd hello-nextrush
pnpm init$ pnpm add nextrush$ pnpm add -D typescript @types/node
Create tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["src"]
}Create Entry File
Create src/index.ts with the code from The Code section above.
Test
curl http://localhost:3000
# {"message":"Hello, NextRush!"}What Each Part Does
createApp()
Creates the NextRush application instance. This manages middleware and plugins.
const app = createApp();createRouter()
Creates a router for defining routes. NextRush uses a radix tree router.
const router = createRouter();Route Definition
Define a GET route at the root path:
router.get('/', (ctx) => {
ctx.json({ message: 'Hello, NextRush!' });
});ctx— The context object containing request and responsectx.json()— Send a JSON response
Mounting Routes
Mount the router on the application at a path prefix:
app.route('/', router);The first argument is the path prefix. All routes defined on the router are served under that prefix.
Starting the Server
Start listening for requests:
await listen(app, 3000);listen() returns a Promise<ServerInstance>. Use await or handle the promise.
Adding More Routes
Compose multiple routers for different resource groups.
Multi-router composition example (click to expand)
import { createApp, createRouter, listen } from 'nextrush';
const app = createApp();
// Root router
const root = createRouter();
root.get('/', (ctx) => {
ctx.json({ message: 'Hello, NextRush!' });
});
// Health router
const health = createRouter();
health.get('/', (ctx) => {
ctx.json({
status: 'ok',
uptime: process.uptime(),
});
});
// Echo router
const echo = createRouter();
echo.get('/', (ctx) => {
ctx.json({
method: ctx.method,
path: ctx.path,
query: ctx.query,
});
});
// Users router with parameters
const users = createRouter();
users.get('/:id', (ctx) => {
ctx.json({ id: ctx.params.id });
});
// Mount all routers
app.route('/', root);
app.route('/health', health);
app.route('/echo', echo);
app.route('/users', users);
await listen(app, 3000);With Middleware
Add common middleware:
import { createApp, createRouter, listen } from 'nextrush';
import { cors } from '@nextrush/cors';
import { json } from '@nextrush/body-parser';
const app = createApp();
// Middleware (runs before all routes)
app.use(cors());
app.use(json());
// Routes
const api = createRouter();
api.get('/', (ctx) => {
ctx.json({ message: 'Hello, NextRush!' });
});
api.post('/echo', (ctx) => {
ctx.json({ received: ctx.body });
});
// Mount
app.route('/api', api);
await listen(app, 3000);$ pnpm add @nextrush/cors @nextrush/body-parser
Production Build
# Build for production
npx nextrush build
# Run production build
node dist/index.jsNext Steps
- REST API Example — Build a complete CRUD API
- Middleware Guide — Learn about middleware
- Routing Guide — Deep dive into routing
- API Reference — Core — Full Application and Context API
- Coming from Express? — Migration guide for Express users