Skip to content

Documentation

micnic edited this page Nov 3, 2012 · 94 revisions
var simples = require('simples');

New simples instance

simples(port)

port: number

simpleS needs only the port number and it sets up a HTTP server on this port.

var server = simples(80);

or

var server = new simples(80);

Server management

Starting and restarting

.start(port, callback)

port: number

callback: function(0)

Start listening for requests on the provided port, if the server is already started, simpleS will restart the server and will listen for the new provided port.

server.start(80, function () {
    // Application logic
});

Stopping

.stop(callback)

callback: function(0)

Stop the server and uses the callback function when server is stopped

server.stop(function () {
    // Application logic
});

Routing

GET requests

.get(route, callback)

route: string

callback: function(2)

Listen for get requests and uses the callback function with request and response as parameters

server.get('/', function (request, response) {
    // Application logic
});

POST requests

.post(route, callback)

route: string

callback: function(2)

Listen for post requests and uses the callback function with request and response as parameters

server.post('/', function (request, response) {
    // Application logic
});

All kind of requests

.all(route, callback)

route: string

callback: function(2)

Listen for all kind of requests and uses the callback function with request and response as parameters

server.all('/', function (request, response) {
    // Application logic
});

Static files

.getStatic(path)

path: string

path is the local path to a folder that contains static files (for example: css and js files), this folder will serve as the root folder for the server. Only one folder should be used to server static files, if more .getStatic() methods will be called only the last will be used to serve static files. The folder with static files can contain other folders, their content will be also served.

server.getStatic('root');

Inexistent routes

.notFound(callback)

callback: function(2)

Use the callback function with request and response as parameters for undefined routes. Only one callback function can be used for inexistent routes, if more .notFound() methods will be called only the last will be used for routing.

server.notFound(function (request, response) {
    // Application logic
});

WebSocket

WebSocket server

.ws(path, config, callback)

path: string

config: object

callback: function(1)

Listen for WebSocket connections. For security reasons origins and protocols should be provided in the config parameter, the length of the message can be limited, default is 1MB. The callback function comes with the connection as parameter.

server.ws('/', {
    messageMaxLength: 1024,
    origins: ['null'],
    protocols: ['chat']
}, function (connection) {
    // Application logic
});

Clone this wiki locally