This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Camp Node.js Web Framework Cheat Sheet

A quick reference guide for Camp, a minimalist web and WebSocket server framework for Node.js: routing, HTML templates, static files, and middlewares.

· Published

Camp is a lightweight, zero-boilerplate web and WebSocket application server framework for Node.js.

Quickstart Server

const Camp = require('camp');

const server = Camp.start({
  port: 8080
});

console.log('Server running on port 8080');

By default, Camp automatically serves static assets located in the ./web/ directory.


Route Handlers

GET Requests & URL Query Params

// Handles /search?q=query
server.path('/search', (req, res) => {
  const query = res.query.q;
  res.json({
    query: query,
    results: []
  });
});

POST & HTTP Verbs

server.post('/api/users', (req, res) => {
  const body = req.body;
  res.json({ status: 'created', user: body });
});

Templates & Views

Compile and render dynamic templates:

const template = Camp.template('./templates/post.html');

server.path('/blog/:post.html', (req, res) => {
  res.template({
    title: 'Post Title',
    content: 'Hello World'
  }, template);
});

404 & Middleware Handlers

Catch-all Fallback

server.notFound('/*', (req, res) => {
  res.file('/404.html');
});

Interceptor Middleware

server.handle((req, res, next) => {
  res.setHeader('X-Powered-By', 'Camp');
  next();
});

WebSockets

server.ws('/chat', (socket) => {
  socket.on('message', (msg) => {
    console.log('Received:', msg);
  });
});

Related topics