Powerful server for Node.js with http proxy middleware support & websocket/client connections**:
// Include it and extract some methods for convenience
const server = require('server');
const { get, post } = server.router;
// Launch server with options and a couple of routes
server({ port: 8080 }, [
get('/', ctx => 'Hello world'),
post('/', ctx => {
console.log(ctx.data);
return 'ok';
})
]);
There's a whole tutorial on getting started for beginners but the quick version is to first install server
as a dependency:
npm install server
Server requires Node.js 7.6.0 or newer. Node.js 8.x.y LTS is recommended.
Then you can create a file called index.js
with this code:
// Include the server in your file
const server = require('server');
const { get, post } = server.router;
// Handle requests to the url "/" ( http://localhost:3000/ )
server([
get('/', ctx => 'Hello world!')
]);
Execute this in the terminal to get the server started:
node .
And finally, open your browser on localhost:7777 and you should see 'Hello world!' on your browser. You can also proxy external connections to localhost:4444 port 4444 (proxy) localhost:8080(proxy-server)
Wrapped with OpenSSL tookit for secured SSL and TCP connections
Socket.IO Socket
Socket.IO enables real-time bidirectional event-based communication. It consists of:
- a Node.js server (this repository)
- a Javascript client library for the browser (or a Node.js client)
Some implementations in other languages are also available:
Its main features are:
Connections are established even in the presence of:
- proxies and load balancers.
- personal firewall and antivirus software.
For this purpose, it relies on Engine.IO, which first establishes a long-polling connection, then tries to upgrade to better transports that are "tested" on the side, like WebSocket. Please see the Goals section for more information.
Unless instructed otherwise a disconnected client will try to reconnect forever, until the server is available again. Please see the available reconnection options here.
A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.
That functionality is achieved with timers set on both the server and the client, with timeout values (the pingInterval
and pingTimeout
parameters) shared during the connection handshake. Those timers require any subsequent client calls to be directed to the same server, hence the sticky-session
requirement when using multiples nodes.
Any serializable data structures can be emitted, including:
- ArrayBuffer and Blob in the browser
- ArrayBuffer and Buffer in Node.js
Sample code:
io.on('connection', socket => {
socket.emit('request', /* … */); // emit an event to the socket
io.emit('broadcast', /* … */); // emit an event to all connected sockets
socket.on('reply', () => { /* … */ }); // listen to the event
});
Starting with 3.0, express applications have become request handler
functions that you pass to http
or http
Server
instances. You need
to pass the Server
to socket.io
, and not the express application
function. Also make sure to call .listen
on the server
, not the app
.
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
vm2 is a sandbox that can run untrusted code with whitelisted Node's built-in modules. Securely!
Features
- Runs untrusted code securely in a single process with your code side by side
- Full control over sandbox's console output
- Sandbox has limited access to process's methods
- Sandbox can require modules (builtin and external)
- You can limit access to certain (or all) builtin modules
- You can securely call methods and exchange data and callbacks between sandboxes
- Is immune to all known methods of attacks
- Transpilers support
How does it work
- It uses internal VM module to create secure context
- It uses Proxies to prevent escaping the sandbox
- It overrides builtin require to control access to modules