DEV Community

Lam
Lam

Posted on

2 2

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay