DEV Community

Lam
Lam

Posted on

Camp Cheat Sheet

Web sockets

camp.ws('/path', (socket) => { ··· })
Enter fullscreen mode Exit fullscreen mode
camp.wsChannels[path]
Enter fullscreen mode Exit fullscreen mode
camp.wsBroadcast('/path', (req, res) => {
})
Enter fullscreen mode Exit fullscreen mode

Sorry I don't completely understand this yet, but check it out in their docs.

See: WebSocket

Implicit templates

camp.path('blog.html')
Enter fullscreen mode Exit fullscreen mode

Uses blog.html as a template.

See: Templates

Advanced features

Basic templates

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

camp.path('/blog/:post.html', (req, res) => {
  res.template({
    text: 'Hello world'
  }, tpl)
})
Enter fullscreen mode Exit fullscreen mode

Low level handler

camp.handle((req, res, next) => {
  res.setHeader('X-Hello', 'world')
  next()
})
Enter fullscreen mode Exit fullscreen mode

See: Handlers

Templates

Not found

camp.notFound('/*.lol', (req, res) => {
  res.file('/404.html')
})
Enter fullscreen mode Exit fullscreen mode

See: Fall through

Templates

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

camp.path('/blog/:post.html', (req, res) => {
  res.template({
    text: 'Hello world'
  }, tpl)
})
Enter fullscreen mode Exit fullscreen mode

See: Templates

Routes

Handles /search?q=rainbows

camp.path('/search', (req, res) => {
  const q = res.query.q
  res.json({ results: ··· })
})
Enter fullscreen mode Exit fullscreen mode

Also available: camp.post, camp.get.

Quick start

app.js

const Camp = require('cs/camp')
const camp = Camp.start({ port: 1234 })
Enter fullscreen mode Exit fullscreen mode

web/index.html

<!doctype html>
<body>Hello world!</body>
Enter fullscreen mode Exit fullscreen mode

Camp serves files in web/ by default.

Reference

Top comments (0)