Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 3f5ab1a

Browse files
committed
Adding new examples ideas.
1 parent 678b3d7 commit 3f5ab1a

File tree

9 files changed

+30
-22
lines changed

9 files changed

+30
-22
lines changed

node/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm run example ts example-name
1616

1717
To run the HTTPS server example, just:
1818
```bash
19-
npm run example ts HttpsServer
19+
npm run example ts HttpServer
2020
```
2121

2222
Under the hood, this examples are running through [ts-node](https://github.com/TypeStrong/ts-node), which is not recommended in production environments. You can also build those examples with:
@@ -36,11 +36,14 @@ node ./dist/example-name.js
3636

3737
## Examples
3838
* [TcpServer](./src/TcpServer.ts) - a simple TCP server
39-
* [HttpsServer](./src/HttpsServer.ts) - a simple HTTPS server
39+
* [HttpServer](./src/HttpServer.ts) - a simple HTTPS server
4040
* [API Client](./src/APIClient.ts) - client that sends a "ping"
4141
* [API Server](./src/APIServer.ts) - server the receives that "ping" and responds with a "pong"
42+
* [Word counter](./src/WordCounter.ts) - shows how many of the desired words are presented in a file
43+
* [Wikipedia Search](./src/Wikipedia.ts) - searches the [Wikipedia](https://en.wikipedia.org/w/api.php?) website
44+
* [Web Scrapping](./src/WebScrapping.ts) - make a web scrapping in the [webscraper.io](https://www.webscraper.io/test-sites/e-commerce/static), pagination included
4245

43-
**note**: due to HTTP/HTTPS distinct way of handle localhost requests, in the API example, HTTP is used instead of HTTPS because is a more simple way to set it up.
46+
**note**: due to HTTP/HTTPS distinct way of handle localhost requests, in the examples HTTP is used instead of HTTPS because is a more easy way to set it up.
4447

4548
## Standards
4649
A modified version of the [Microsoft Linter Standards](https://github.com/Microsoft/tslint-microsoft-contrib) is used. Please be mindful that they are here to help you out improve you code.

node/src/APIClient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const handleResponse = (resolve: (data: string) => void, reject: (data: Error) =
1515
.on('error', reject)
1616
.on('uncaughtException', reject)
1717
.on('data', (data: string) => chunk += data)
18-
.on('end', () => resolve(chunk));
18+
.on('end', () => { resolve(chunk); });
1919
};
2020

21-
const ping = (): Promise<string> => new Promise((resolve: (data: string) => void, reject: (data: Error) => void) => {
21+
const ping = async (): Promise<string> => new Promise((resolve: (data: string) => void, reject: (data: Error) => void) => {
2222
const post = request({
2323
path: '/',
2424
port: 8080,
@@ -28,11 +28,11 @@ const ping = (): Promise<string> => new Promise((resolve: (data: string) => void
2828
'Content-Type': 'text/plain'
2929
}
3030
});
31-
const curriedHandleResponse = ((response: IncomingMessage) => handleResponse(resolve, reject, response));
31+
const curriedHandleResponse = ((response: IncomingMessage) => { handleResponse(resolve, reject, response); });
3232

3333
post.write('ping');
3434
post.on('response', curriedHandleResponse);
35-
post.on('error', () => reject(new Error('Request error')));
35+
post.on('error', () => { reject(new Error('Request error')); });
3636
post.end();
3737
});
3838

node/src/APIServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const server = createServer((sent: IncomingMessage, res: ServerResponse) => {
77
const body = <Buffer[]> [];
88

99
sent.on('data', (data: Buffer) => body.push(data));
10-
sent.on('error', (err) => console.error(err));
10+
sent.on('error', (err) => { console.error(err); });
1111
sent.on('end', () => {
1212
const message = Buffer.concat(body).toString();
1313

@@ -18,4 +18,4 @@ const server = createServer((sent: IncomingMessage, res: ServerResponse) => {
1818
res.end('pong');
1919
});
2020

21-
server.listen(8080, 'localhost', () => console.table('Server running at https://localhost:8080/'));
21+
server.listen(8080, 'localhost', () => { console.log('Server running at https://localhost:8080/'); });

node/src/HttpServer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* HTTPS server example.
3+
*/
4+
import { createServer, IncomingMessage, ServerResponse } from 'http';
5+
6+
const host = 'localhost';
7+
const port = 1337;
8+
9+
const server = createServer((_: IncomingMessage, res: ServerResponse) => {
10+
res.writeHead(200, { 'Content-Type': 'text/plain' });
11+
res.end('Hello World\n');
12+
});
13+
14+
server.listen(port, host, () => { console.log(`Server running at ${host}:${port}`); });

node/src/HttpsServer.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

node/src/TcpServer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
*/
44
import { createServer, Socket } from 'net';
55

6+
const host = 'localhost';
7+
const port = 1337;
8+
69
const server = createServer((socket: Socket) => {
710
socket.write('Echo server\r\n');
811
socket.pipe(socket);
912
});
1013

11-
server.listen(1337, '127.0.0.1', () => console.log('Server running at http://127.0.0.1:1337/'));
14+
server.listen(port, host, () => { console.log(`Server running at ${host}:${port}`); });

node/src/WebScrapping.ts

Whitespace-only changes.

node/src/Wikipedia.ts

Whitespace-only changes.

node/src/WordCounter.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)