I am pretty sure most of you (developers) are totally in love with the terminal or command-line. Using the terminal makes you cool because of the simple reason that you do not use the mouse and complete everything with they keyboard. How cool is that ?!
Have you ever imagined how interesting it would be to use the same terminal from the browser as a web application ? Well its beautiful too, but the catch here is that developing this would be kind of tough. Believe me with the introduction of node.js things are very easy.
In this post I will be discussing how easy it is to develop a web interactive shell using node.js
Assumptions:
1. You are a basic terminal user.
2. You know the basics of node.js
3. You have node.js installed.
Technical Assumptions:
1. You have express and socket.io modules installed
Now that node.js is up and running at say port 4990.
The below part is a the server side stuff which use a child process to execute the commands you send to it.
var app = require('express').createServer(),
io = require('socket.io').listen(app),
sys = require('util'),
exec = require('child_process').exec;
app.listen(4990);
io.sockets.on('connection', function(socket) {
socket.on('console', function(command, callBack) {
console.log(command.command);
function puts(error, stdout, stderr) {
socket.emit('com', stdout);
}
exec(command.command, puts);
});
});
Have you ever imagined how interesting it would be to use the same terminal from the browser as a web application ? Well its beautiful too, but the catch here is that developing this would be kind of tough. Believe me with the introduction of node.js things are very easy.
In this post I will be discussing how easy it is to develop a web interactive shell using node.js
Assumptions:
1. You are a basic terminal user.
2. You know the basics of node.js
3. You have node.js installed.
Technical Assumptions:
1. You have express and socket.io modules installed
Now that node.js is up and running at say port 4990.
The below part is a the server side stuff which use a child process to execute the commands you send to it.
var app = require('express').createServer(),
io = require('socket.io').listen(app),
sys = require('util'),
exec = require('child_process').exec;
app.listen(4990);
io.sockets.on('connection', function(socket) {
socket.on('console', function(command, callBack) {
console.log(command.command);
function puts(error, stdout, stderr) {
socket.emit('com', stdout);
}
exec(command.command, puts);
});
});
The below is the client side stuff which connects to node server, opens a socket and sends information over the socket. What is nodestrap[1] ?
$.nodestrap({
url: 'http://localhost:4990/',
reconnect_time: 5000,
objectName: 'node',
methods: {
connect: function() {
//alert('onConnecnt');
},
com: function(obj) {
// response from the server which is the result of the commands
},
disconnect: function() {
//alert('disconnected');
}
}
});
reconnect_time: 5000,
objectName: 'node',
methods: {
connect: function() {
//alert('onConnecnt');
},
com: function(obj) {
// response from the server which is the result of the commands
},
disconnect: function() {
//alert('disconnected');
}
}
});
This is used to send the message over the socket from client to server side.
$('#exec').click(function() {
var msg = {};
msg.command = 'ls -al'
emit({
'console': msg
});
});
var msg = {};
msg.command = 'ls -al'
emit({
'console': msg
});
});
[1] : nodestrap is my own plugin which helps the client side in connecting to the node server. The details will be available in the following blog.
I hope this helps you to create a simple web interactive shells. Please improvise ;)