Camp Cheat Sheet

From WikiOD

Here is the cheatsheat of Camp and basic guide on how to use Camp

Getting started[edit | edit source]

{: .-three-column}

Quick start[edit | edit source]

{: .-prime}

app.js[edit | edit source]

{: .-file}

const Camp = require('camp')
const camp = Camp.start({ port: 1234 })

web/index.html[edit | edit source]

{: .-file}

<!doctype html>
<body>Hello world!</body>

Camp serves files in web/ by default.

Routes[edit | edit source]

Handles /search?q=rainbows[edit | edit source]

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

{: data-line=“2”}

Also available: camp.post, camp.get.

Templates[edit | edit source]

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

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

{: data-line=“1,4”}

See: Templates

Not found[edit | edit source]

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

{: data-line=“1”}

See: Fall through

Low level handler[edit | edit source]

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

{: data-line=“1”}

See: Handlers

Templates[edit | edit source]

Basic templates[edit | edit source]

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

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

{: data-line=“1,4,5,6”}

Implicit templates[edit | edit source]

camp.path('blog.html')

Uses blog.html as a template.

See: Templates

Advanced features[edit | edit source]

Web sockets[edit | edit source]

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

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

See: WebSocket

Credit:rstacruz