Camp e un framework server per applicazioni web e WebSocket leggero e immediato per Node.js.
Server di avvio rapido (Quickstart Server)
const Camp = require('camp');
const server = Camp.start({
port: 8080
});
console.log('Server running on port 8080');
Per impostazione predefinita, Camp distribuisce automaticamente i file statici dalla directory ./web/.
Gestori di route (Route Handlers)
Richieste GET e parametri di query URL
// Gestisce /search?q=query
server.path('/search', (req, res) => {
const query = res.query.q;
res.json({
query: query,
results: []
});
});
POST e verbi HTTP
server.post('/api/users', (req, res) => {
const body = req.body;
res.json({ status: 'created', user: body });
});
Template e viste (Templates & Views)
Compilazione e rendering di template dinamici:
const template = Camp.template('./templates/post.html');
server.path('/blog/:post.html', (req, res) => {
res.template({
title: 'Post Title',
content: 'Hello World'
}, template);
});
Gestori 404 e Middleware (404 & Middleware Handlers)
Fallback globale (Catch-all)
server.notFound('/*', (req, res) => {
res.file('/404.html');
});
Middleware intercettore
server.handle((req, res, next) => {
res.setHeader('X-Powered-By', 'Camp');
next();
});
WebSockets
server.ws('/chat', (socket) => {
socket.on('message', (msg) => {
console.log('Received:', msg);
});
});