simpleS is a simple web framework for Node.JS designed to create HTTP(S) servers and clients with WebSocket support and other special features:
- High performance and simple structure with minimum configuration
- Advanced routing for http requests, static files and errors
- Unique interface for requests and responses (named as connection)
- WebSocket implementation (RFC 6455)
- Server mirroring
- Virtual Hosting
- Response compression (deflate and gzip)
- CORS support
- Sessions
- Logging
- Middlewares
- Template engine support
- Node.js client API for HTTP requests and WebSocket connections
Documentation (work in progress)
npm install simples@alpha
constsimples=require('simples');constserver=simples();// Your server is set up on port 80// Catch 404 Errorserver.error(404,(connection)=>{connection.end('Error 404 caught');});// Create the first routeserver.get('/',(connection)=>{connection.end('Simples Works');});consthost0=server;// The server is in the same time the main hostconsthost1=server.host('example.com');// Other hostsconsthost2=server.host('example2.com');// Now for each host you can apply individual routinghost0.get('/',(connection)=>{connection.end('Main Host');});host1.get('/',(connection)=>{connection.end('Host 1');});host2.get('/',(connection)=>{connection.end('Host 2');});Let's create an echo WebSocket server:
server.ws('/',{limit: 1024,// The maximum size of a messageadvanced: false,// Set connection advanced mode, see docs for more infoorigins: ['null']// Set accepted origins, "null" for localhost},(connection)=>{// Log the new connectionconsole.log('New connection');// Listen for messages to send them backconnection.on('message',(message)=>{console.log('Message: '+message.data);connection.send(message.data);});// Log connection closeconnection.on('close',()=>{console.log('Connection closed');});});Access the server from the browser built-in WebSocket API:
constsocket=newWebSocket('ws://localhost/','echo');// Listen for messagessocket.onmessage=(event)=>{console.log(event.data);};// Send the first messagesocket.send('ECHO');Access the server using simples-ws:
importwsfrom'simples-ws';constsocket=ws('/',{protocols: ['echo']});// Listen for messagessocket.on('message',(message)=>{console.log(message.data);});// Send the first messagesocket.send('ECHO');Access the server from server-side simpleS client WebSocket API:
constsimples=require('simples');constclient=simples.client();constsocket=client.ws('/');// Listen for messagessocket.on('message',(message)=>{console.log(message.data);});// Send the first messagesocket.send('ECHO');constsimples=require('simples');constclient=simples.client();// GET requestclient.get('/').on('body',(response,body)=>{console.log('Response status: '+response.status);console.log('Response body: '+body.toString());});// POST requestclient.post('/send').send(/* data */).on('response',(response)=>{// Do something with the response}).on('body',(response,body)=>{console.log('Response body: '+body.toString());});