Camp

Camp is a Node.js web server framework. This guide targets Camp v17.x.

A cheatsheet by @rstacruz|Refreshed 7 months ago.Refresh|View source on Github

Quick start

Quick start

app.js

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

web/index.html

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

Camp serves files in web/ by default.

Routes

Routes

Handles /search?q=rainbows

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

Also available: camp.post, camp.get.

Templates

Templates

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

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

See: Templates

Not found

Not found

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

See: Fall through

Low level handler

Low level handler

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

See: Handlers

Basic templates

Basic templates

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

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

Implicit templates

Implicit templates

camp.path('blog.html')

Uses blog.html as a template.

See: Templates

Web sockets

Web sockets

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