DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
LΓ’m
LΓ’m

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)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: