# Simple Servers

## Basic Server

{% code title="server-basic.js" %}

```javascript
'use strict'

const http = require("http")
let count = 0
http.createServer(function(req, res) {
    const message = `Received ${++count} requests so far`
    console.log(message)
    res.writeHead(200, { "Content-Type": "text/plain" })
    res.end(message)
})
.listen(8080)
```

{% endcode %}

Run it: `node server-basic.js`

Test it: `curl http://localhost:8080`

## Server with Pause

{% code title="server-pause.js" %}

```javascript
'use strict'

const http = require('http')
http.createServer(function (req, res) {
	res.writeHead(200, { 'Content-Type': 'text/plain' })
	res.write('hello\n')
	setTimeout(function () {
		res.end('world\n')
	}, 2 * 1000)
}).listen(8080)

```

{% endcode %}

Run it: `node server-pause.js`

Bench it: `ab -n 100 -c 100 http://localhost:8080/`

## Echo Server

{% code title="server-echo.js" %}

```javascript
'use strict'

const net = require('net')
net.createServer(function (socket) {
	socket.write('Talk to me.\n')
	socket.on('data', function (data) {
		socket.write(data.toString().toUpperCase())
	})
}).listen(8080)
```

{% endcode %}

Run it: `node server-echo.js`

Talk with it: `nc localhost 8080`

## Chat Server

{% code title="server-chat.js" %}

```javascript
'use strict'

// Application
const net = require('net')
let sockets = []
net.createServer(function (socket) {
	sockets.push(socket)
	socket.write('Talk to me.\n')

	socket.on('data', function (data) {
		for (let i = 0; i < sockets.length; ++i) {
			sockets[i].write(data)
		}
	})

	socket.on('end', function () {
		const index = sockets.indexOf(socket)
		sockets = sockets.slice(0, index).concat(sockets.slice(index + 1))
	})
}).listen(8080)
```

{% endcode %}

Run it: `node server-chat.js`

Talk with it: `nc localhost 8000`

## Supplemental

### Echo Server with Transform Stream

{% code title="server-echo-transform.js" %}

```javascript
'use strict'

class UC extends require('stream').Transform {
	_transform (data, encoding, next) {
		next(null, data.toString().toUpperCase())
	}
}

class Reverse extends require('stream').Transform {
	_transform (data, encoding, next) {
		next(null, data.toString().split('').reverse().join(''))
	}
}

require('net').createServer(function (socket) {
	socket.write('Talk to me.\n')
	socket.pipe(new UC()).pipe(new Reverse()).pipe(socket)
}).listen(8080)
```

{% endcode %}

Run it: `node server-echo-transform.js`

Talk with it: `nc localhost 8080`

### Chat Server with Extras

{% code title="server-chat-extra.js" %}

```javascript
'use strict'

// Application
const net = require('net')
let count = 0
const sockets = {}
function broadcast (message) {
	for (const key in sockets) {
		if (sockets.hasOwnProperty(key)) {
			sockets[key].write(message)
		}
	}
}
net.createServer(function (socket) {
	sockets[socket.index = count++] = socket
	socket.write('Hello user ' + socket.index + '\n')
	broadcast('User ' + socket.index + ' joined\n')

	socket.on('data', function (data) {
		broadcast('User ' + socket.index + ' says: ' + data.toString())
	})

	socket.on('end', function () {
		delete sockets[socket.index]
		broadcast('User ' + socket.index + ' left\n')
	})
}).listen(8080)
```

{% endcode %}

Run it: `node server-chat-extra.js`

Talk with it: `nc localhost 8080`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.bevry.me/hands-on-with-node.js/simple-servers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
