Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/abourass/nodejs-server-examples

A few short examples of different server configurations for Node.JS
https://github.com/abourass/nodejs-server-examples

api example examples node node-js nodejs server stream

Last synced: 4 days ago
JSON representation

A few short examples of different server configurations for Node.JS

Awesome Lists containing this project

README

        

# Node Server Examples
These are some simple networking examples for NodeJS that I learned while reading *Networking Patterns* by *Pedro Teixeira*

### Table of Contents
Example | Description
------- | -----------
#1 | [Raw Server](#1)
#2 | [Flawed Basic Service](#2)
#3 | [Basic Service as a Stream](#3)
#4 | [JSON Streams and Event Gateways](#4)

## Installation:
Download and open your preferred terminal within the folder, then run:

`npm i`

## Running the Examples:
You can use any of the run commands in the package.json file


## #1: Raw Server
An extremely basic TCP echo server. Run:

`node rawServer.js`

Now that the server is running use Telnet or NetCat to connect:

Windows:
```telnet localhost 9000```

Linux/Unix:
```nc localhost 9000```

Once connected, type whatever you want. In the terminal window which you started running the server you should see log statements that log when you connect, and the raw buffer data (printing out each byte). If you are curious what it does, just open the file, I've commented enough to highlight how it works.


## #2: A Flawed Basic Service
A service which returns the provided string in all caps. This service was built with a flaw, it provides no back-pressure. If the rate at which a client connection sends characters is lower than the rate it receives (for instance, if the downstream available bandwidth is higher than the upstream one), the process memory will grow until it is exhausted. What's needed here is a mechanism by which the incoming TCP stream is paused if the level of memory pending on the outgoing buffer gets high enough. Run via:

```node capitalizingServer.js```

Now that the server is running use Telnet or NetCat to connect:

Windows:
```telnet localhost 9000```

Linux/Unix:
```nc localhost 9000```

Once connected, type whatever you want, and it will be returned in all caps.


## #3: A Basic Service as a Stream
This is an implementation of the same capitalization service as #2, however, it's implemented as a stream in order to solve the flaw outlined in example #2. So, rather than listening for TCP 'data' events we start a stream, then pipe the connection into the stream, and finally, pipe the stream back into the service. Run via:

```node capitalizingServer2.js```

Now that the server is running use Telnet or NetCat to connect:

Windows:
```telnet localhost 9000```

Linux/Unix:
```nc localhost 9000```

Once connected, type whatever you want, and it will be returned in all caps.


## #4: JSON Streams and Event Gateways
In this example the TCP server accepts events from domotic devices (thermometers, lights, etc..) in the form of a JSON object.
This example uses the '*json-duplex-stream*' package from NPM in order to create a service that accepts and replies with a streamable format of JSON.
JSON isn't particularly stream-friendly by default, as JSON is only valid once the entire object is transmitted, making a stream somewhat useless. However, if none of the transmitted objects, when serialised, contain new-line characters, we can safely separate each one of the main objects with a new line character. The default Node.js JSON encoding doesn't introduce any new-line character, and for those existing in strings within the object, it encodes them with *"\n"*.

Example:
```
{"a":1,"b":true,c:"this is a newline-terminated string\n"}
{"d":2,"e":false,f:"this is another newline-terminated string\n"}
```
Back to our event gateway example, each device will connect to this service and send it events throughout time. The gatway service saves each of these events into a persisted queue for further processing by other process. Each recieved object represents an event, and each event have a unique `id` field. Once done saving the event into the queue, the gateway replies with a message confirming that the events were saved. Run via:

```node gatewayServer.js```

Now that the server is running use Telnet or NetCat to connect:

Windows:
```telnet localhost 8000```

Linux/Unix:
```nc localhost 8000```

Once connected, type: `{"when":"2019-02-25T14:28:49-07:00", "type":"temperature", "reading":69.8, "units":"F", "id":"5f18453d-1907-48bc-abd2-ab6c24bc197d"}`

You should get back a confirmation like: `{"id":"5f18453d-1907-48bc-abd2-ab6c24bc197d", "success":true}`