Simple Servers

Basic Server

server-basic.js
1
'use strict'
2
3
const http = require("http")
4
let count = 0
5
http.createServer(function(req, res) {
6
const message = `Received ${++count} requests so far`
7
console.log(message)
8
res.writeHead(200, { "Content-Type": "text/plain" })
9
res.end(message)
10
})
11
.listen(8080)
Copied!
Run it: node server-basic.js
Test it: curl http://localhost:8080

Server with Pause

server-pause.js
1
'use strict'
2
3
const http = require('http')
4
http.createServer(function (req, res) {
5
res.writeHead(200, { 'Content-Type': 'text/plain' })
6
res.write('hello\n')
7
setTimeout(function () {
8
res.end('world\n')
9
}, 2 * 1000)
10
}).listen(8080)
11
Copied!
Run it: node server-pause.js
Bench it: ab -n 100 -c 100 http://localhost:8080/

Echo Server

server-echo.js
1
'use strict'
2
3
const net = require('net')
4
net.createServer(function (socket) {
5
socket.write('Talk to me.\n')
6
socket.on('data', function (data) {
7
socket.write(data.toString().toUpperCase())
8
})
9
}).listen(8080)
Copied!
Run it: node server-echo.js
Talk with it: nc localhost 8080

Chat Server

server-chat.js
1
'use strict'
2
3
// Application
4
const net = require('net')
5
let sockets = []
6
net.createServer(function (socket) {
7
sockets.push(socket)
8
socket.write('Talk to me.\n')
9
10
socket.on('data', function (data) {
11
for (let i = 0; i < sockets.length; ++i) {
12
sockets[i].write(data)
13
}
14
})
15
16
socket.on('end', function () {
17
const index = sockets.indexOf(socket)
18
sockets = sockets.slice(0, index).concat(sockets.slice(index + 1))
19
})
20
}).listen(8080)
Copied!
Run it: node server-chat.js
Talk with it: nc localhost 8000

Supplemental

Echo Server with Transform Stream

server-echo-transform.js
1
'use strict'
2
3
class UC extends require('stream').Transform {
4
_transform (data, encoding, next) {
5
next(null, data.toString().toUpperCase())
6
}
7
}
8
9
class Reverse extends require('stream').Transform {
10
_transform (data, encoding, next) {
11
next(null, data.toString().split('').reverse().join(''))
12
}
13
}
14
15
require('net').createServer(function (socket) {
16
socket.write('Talk to me.\n')
17
socket.pipe(new UC()).pipe(new Reverse()).pipe(socket)
18
}).listen(8080)
Copied!
Run it: node server-echo-transform.js
Talk with it: nc localhost 8080

Chat Server with Extras

server-chat-extra.js
1
'use strict'
2
3
// Application
4
const net = require('net')
5
let count = 0
6
const sockets = {}
7
function broadcast (message) {
8
for (const key in sockets) {
9
if (sockets.hasOwnProperty(key)) {
10
sockets[key].write(message)
11
}
12
}
13
}
14
net.createServer(function (socket) {
15
sockets[socket.index = count++] = socket
16
socket.write('Hello user ' + socket.index + '\n')
17
broadcast('User ' + socket.index + ' joined\n')
18
19
socket.on('data', function (data) {
20
broadcast('User ' + socket.index + ' says: ' + data.toString())
21
})
22
23
socket.on('end', function () {
24
delete sockets[socket.index]
25
broadcast('User ' + socket.index + ' left\n')
26
})
27
}).listen(8080)
Copied!
Run it: node server-chat-extra.js
Talk with it: nc localhost 8080
Last modified 2yr ago