From 20aefb510561e1a04e4f29327605d133d3eaeb1d Mon Sep 17 00:00:00 2001 From: Patrick Heneise Date: Thu, 11 May 2023 18:14:28 -0700 Subject: [PATCH 1/5] feat: add simple http server example --- servers/http/README.md | 13 +++++++++++++ servers/http/index.js | 29 +++++++++++++++++++++++++++++ servers/http/package.json | 13 +++++++++++++ servers/http/test/http.test.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 servers/http/README.md create mode 100644 servers/http/index.js create mode 100644 servers/http/package.json create mode 100644 servers/http/test/http.test.js diff --git a/servers/http/README.md b/servers/http/README.md new file mode 100644 index 00000000..62a42841 --- /dev/null +++ b/servers/http/README.md @@ -0,0 +1,13 @@ +# Simple HTTP Server + +## Usage + +```bash +npm start +``` + +## Test + +```bash +npm test +``` diff --git a/servers/http/index.js b/servers/http/index.js new file mode 100644 index 00000000..bc1ebae5 --- /dev/null +++ b/servers/http/index.js @@ -0,0 +1,29 @@ +import http from 'node:http' + +const server = http.createServer() +server.on('request', (request, reply) => { + switch (request.url) { + case '/': + reply.writeHead(200, { 'Content-Type': 'application/json' }) + reply.end( + JSON.stringify({ + data: 'Hello World!' + }) + ) + break + case '/api': + reply.writeHead(200, { 'Content-Type': 'application/json' }) + reply.end( + JSON.stringify({ + data: 'Awesome API!' + }) + ) + break + default: + reply.writeHead(404, { 'Content-Type': 'application/json' }) + reply.end(JSON.stringify({})) + } +}) +server.listen(3000) + +export default server diff --git a/servers/http/package.json b/servers/http/package.json new file mode 100644 index 00000000..dff00e71 --- /dev/null +++ b/servers/http/package.json @@ -0,0 +1,13 @@ +{ + "name": "node-example-http", + "version": "1.0.0", + "description": "A simple http server with Node.js", + "type": "module", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "node --test" + }, + "keywords": [], + "author": "Patrick Heneise " +} diff --git a/servers/http/test/http.test.js b/servers/http/test/http.test.js new file mode 100644 index 00000000..a9bfc5b2 --- /dev/null +++ b/servers/http/test/http.test.js @@ -0,0 +1,31 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import server from '../index.js' + +test('http', async (t) => { + await t.test('/', async () => { + try { + const request = await fetch('http://localhost:3000') + const actual = await request.json() + const expected = { data: 'Hello World!' } + + assert.deepEqual(actual, expected) + } catch (error) { + assert.fail(error) + } + }) + + await t.test('/api', async () => { + try { + const request = await fetch('http://localhost:3000/api') + const actual = await request.json() + const expected = { data: 'Awesome API!' } + + assert.deepEqual(actual, expected) + } catch (error) { + assert.fail(error) + } + }) + + t.after(() => server.close()) +}) From dddc2003b9166ddb74fbac64f9ae72af506c1e9e Mon Sep 17 00:00:00 2001 From: Patrick Heneise Date: Thu, 11 May 2023 18:16:03 -0700 Subject: [PATCH 2/5] chore: sort package json --- servers/http/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/servers/http/package.json b/servers/http/package.json index dff00e71..ca412148 100644 --- a/servers/http/package.json +++ b/servers/http/package.json @@ -2,12 +2,12 @@ "name": "node-example-http", "version": "1.0.0", "description": "A simple http server with Node.js", + "keywords": [], + "author": "Patrick Heneise ", "type": "module", "main": "index.js", "scripts": { "start": "node index.js", "test": "node --test" - }, - "keywords": [], - "author": "Patrick Heneise " + } } From c5ede85b4c0b26d6414d047db16fe4bc8c5e8303 Mon Sep 17 00:00:00 2001 From: Patrick Heneise Date: Tue, 23 May 2023 15:48:43 -0700 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Augustin Mauroy --- servers/http/index.js | 4 +++- servers/http/package.json | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/servers/http/index.js b/servers/http/index.js index bc1ebae5..34fc8e58 100644 --- a/servers/http/index.js +++ b/servers/http/index.js @@ -1,6 +1,7 @@ import http from 'node:http' const server = http.createServer() + server.on('request', (request, reply) => { switch (request.url) { case '/': @@ -24,6 +25,7 @@ server.on('request', (request, reply) => { reply.end(JSON.stringify({})) } }) -server.listen(3000) + +server.listen(3000, () => console.log('serveur work at http://localhost:3000')) export default server diff --git a/servers/http/package.json b/servers/http/package.json index ca412148..8c7bebdf 100644 --- a/servers/http/package.json +++ b/servers/http/package.json @@ -9,5 +9,8 @@ "scripts": { "start": "node index.js", "test": "node --test" + }, + "engines": { + "node": "20" } } From da749f5afae908c78421806d111677aa779a2016 Mon Sep 17 00:00:00 2001 From: Patrick Heneise Date: Mon, 29 May 2023 16:28:31 -0700 Subject: [PATCH 4/5] chore: README update --- servers/http/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/servers/http/README.md b/servers/http/README.md index 62a42841..fff1ffb6 100644 --- a/servers/http/README.md +++ b/servers/http/README.md @@ -1,5 +1,21 @@ # Simple HTTP Server +This example demnstrates a (very) simple HTTP server with Node.js. It uses the +`http` module to create a server and listen on port 3000 for connections. There +are two routes defined: + +- `/` +- `/api` + +Both return a simple +[JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) +object. + +Keep in mind this is a minimal example. It does not handle errors, or any other +[HTTP methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) than +`GET`. For a production API, you should consider a framework like +[Fastify](https://www.fastify.io/). + ## Usage ```bash @@ -8,6 +24,8 @@ npm start ## Test +`http.test.js` shows how to test the server using the native `node:test` module. + ```bash npm test ``` From e905126e140ae8de53e6fec63a0d0aa89bbb56d2 Mon Sep 17 00:00:00 2001 From: Patrick Heneise Date: Tue, 30 May 2023 08:23:05 -0700 Subject: [PATCH 5/5] fix: add links to mdn and node docs --- servers/http/README.md | 5 ++++- servers/http/test/http.test.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/servers/http/README.md b/servers/http/README.md index fff1ffb6..8126f069 100644 --- a/servers/http/README.md +++ b/servers/http/README.md @@ -24,7 +24,10 @@ npm start ## Test -`http.test.js` shows how to test the server using the native `node:test` module. +`http.test.js` shows how to test the server using the native +[node:test](https://nodejs.org/dist/latest-v20.x/docs/api/test.html#test-runner) +module and the +[fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API. ```bash npm test diff --git a/servers/http/test/http.test.js b/servers/http/test/http.test.js index a9bfc5b2..4f916dfd 100644 --- a/servers/http/test/http.test.js +++ b/servers/http/test/http.test.js @@ -5,6 +5,7 @@ import server from '../index.js' test('http', async (t) => { await t.test('/', async () => { try { + // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API const request = await fetch('http://localhost:3000') const actual = await request.json() const expected = { data: 'Hello World!' }