January 2018
Intermediate to advanced
336 pages
7h 22m
English
So far in this chapter, we’ve explored the server side of Node.js sockets. Here we’ll write a client program in Node.js to receive JSON messages from our net-watcher-json-service program. We’ll start with a naive implementation, and then improve upon it through the rest of the chapter.
Open an editor and insert this:
| | 'use strict'; |
| | const net = require('net'); |
| | const client = net.connect({port: 60300}); |
| | client.on('data', data => { |
| | const message = JSON.parse(data); |
| | if (message.type === 'watching') { |
| | console.log(`Now watching: ${message.file}`); |
| | } else if (message.type === 'changed') { |
| | const date = new Date(message.timestamp); |
| | console.log( ... |
Read now
Unlock full access