Skip to content

Instantly share code, notes, and snippets.

@espadrine
Last active March 20, 2016 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espadrine/909be38537dd06d3a4dc to your computer and use it in GitHub Desktop.
Save espadrine/909be38537dd06d3a4dc to your computer and use it in GitHub Desktop.
Potential new ScoutCamp API
// Server demo. `node app [PORT [https]]`
// © 2011-2016 Thaddée Tyl, Jan Keromnes. LGPL.
let port = +process.argv[2] || +process.env.PORT || 1234
let camp = require('./lib/camp.js')
let sc = camp.start({port: port, secure: process.argv[3] === 'https'})
console.log('http://[::1]:' + port)
// Templating demo: /template.html?title=Hello&info=[Redacted].
sc.path('template.html')
// Templating demo with multiple templates and path parameter.
// /html.template/Hello/World
let template = camp.Template('template.html')
let flip = camp.Template('flip.html')
sc.path('html.template/:title/:info', (req, res) => {
res.template(req.data, [template, flip])
})
// Doctor demo: /doctor?text=…
let replies = ['Ok.', 'Oh⁉', 'Is that so?', 'How interesting!',
'Hm…', 'What do you mean?', 'So say we all.']
sc.post('doctor', (req, res) => {
replies.push(req.query.text)
res.end({reply: replies[Math.random() * replies.length|0]})
})
// Chat demo
let chat = sc.eventSource('all')
sc.post('talk', (req, res) => {chat.send(req.data); res.end()})
// WebSocket chat demo
sc.wsBroadcast('chat', (req, res) => res.end(req.data))
// Not found demo
sc.notFound('*.lol', (req, res) => res.file('/404.html'))
// Basic authentication demo
sc.get('secret', (req, res) => {
if (req.username === 'Caesar' && req.password === '1234') {
res.end('Congrats, you found it!')
} else {
res.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic')
res.end('Nothing to hide here!')
}
})
// Low-level handler
sc.handle((req, res, down) => {
res.setHeader('X-Open-Source', 'https://github.com/espadrine/sc/')
down()
})
// Server demo. Run this with node to start your server.
// Copyright © 2011-2015 Thaddee Tyl, Jan Keromnes. All rights reserved.
// Code covered by the LGPL license.
// Let's rock'n'roll!
var port = +process.argv[2] || +process.env.PORT || 1234;
var camp = require('./lib/camp.js').start({
port: port,
secure: process.argv[3] === 'secure',
})
, ajax = camp.ajax
console.log('http://[::1]:' + port);
// Templating demo
camp.route('/template.html', function(data, match, end) {
end({ // Try http://localhost/template.html?title=Hello&info=[Redacted].
title: data.title || 'Success'
, info: data.info || 'This document has been templated!'
})
})
// Templating demo with multiple templates
camp.route('/html.template', function(data, match, end) {
end({
title: data.title || 'Success'
, info: data.info || 'This document has been templated!'
}, {
template: ['template.html', 'flip.html']
})
})
// Doctor demo
var replies = ['Ok.', 'Oh⁉', 'Is that so?', 'How interesting!'
,'Hm…', 'What do you mean?', 'So say we all.']
ajax.on('doctor', function(data, end) {
replies.push(data.text)
end({reply:replies[Math.floor(Math.random() * replies.length)]})
})
// Chat demo
var chat = camp.eventSource('all')
ajax.on('talk', function(data, end) {chat.send(data); end()})
// WebSocket chat demo
camp.wsBroadcast('chat', function(data, end) {end(data)})
// Not found demo
camp.notfound(/.*\.lol$/, function(data, match, end) {
end(null, { template: '/404.html' })
})
// Basic authentication demo.
camp.route(/^\/secret$/, function(data, match, end, ask) {
if (ask.username === 'Caesar' && ask.password === '1234') {
end(null, {template: streamFromString('Congrats, you found it!')})
} else {
ask.res.statusCode = 401;
ask.res.setHeader('WWW-Authenticate', 'Basic')
end(null, {template: streamFromString('Nothing to hide here!')})
}
})
// Low-level handler
camp.handler(function(ask) { ask.res.setHeader('X-Open-Source', 'https://github.com/espadrine/sc/'); })
var stream = require('stream')
function streamFromString(str) {
var newStream = new stream.Readable()
newStream._read = function() { newStream.push(str); newStream.push(null) }
return newStream
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment