A
A
Abra Kadabra2016-03-12 04:55:32
JavaScript
Abra Kadabra, 2016-03-12 04:55:32

How to create a Node.js server, [statefull, stateless, echo]?

Good afternoon.
There was a need to make a server, on node.js, which will recognize simple commands and respond to them.
You need to use the network module.
I see Node.js for the first time, to which they answered: "well, you make websites."
I tried various combinations from the tutorials. In order to force the server to respond at least to something. At the beginning, everything looked like this (in order to get at least some kind of response):

var http = require('http');
 
http.createServer(function(request, response) {
    response.writeHead(200);
    request.pipe(response);
}).listen(8080);

The server was silent, nothing was displayed on the page. With any input (I have a poppy).
Then I started using the network module. Piece off. tutorials and the server started, cursed and that's it.
const net = require('net');

var server = net.createServer(function(socket) {
  socket.end('goodbye\n');
}).on('error', function(err) {
    
  throw err;
});

server.listen(function(){
  address = server.address();
  console.log('opened server on %j', address);
});

The server issued the IP address and port, of course. But in no modules, I did not find line-by-line reading from the terminal.
As a result, the server is expected to communicate like this:
-->open (client request)
  <--opened (server response)
  -->add
  <--added
  -->process
  <--processed

Can I add some custom functions to handle input? (like readln )
After that, issue some kind of std.output using no module.
What will it represent
stateful & stateless
server?
The only difference is that the second one will remember the history of the commands it processed, right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2016-03-12
@Jmaster

This is roughly how it's done:

'use strict';
const net = require('net');

const server = net.createServer(socket => {
    var data = '';
    socket.on('data', d => {
        data += d;
        var p = data.indexOf('\n');
        if(~p) {
            let cmd = data.substr(0, p);
            data = data.slice(p + 1);
            onCommand(cmd.trim(), socket);
        }
    });
});

server.listen(() => {
    var address = server.address();
    console.log('opened server on', address);
});

function onCommand(cmd, socket) {
    switch(cmd) {
        case 'open':
            socket.write('opened\n');
            break;
        case 'add':
            socket.write('added\n');
            break;
        case 'process':
            socket.write('processed\n');
            break;
    }
}

tested via telnet, works

I
Itvanya, 2016-03-12
@Itvanya

It's a regular socket on a tcp server :) created a tcp server that listens, creates a client that makes a request - half the work has already been done. You can process commands from the command line using process.argv.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question