diff --git a/servers/http/README.md b/servers/http/README.md new file mode 100644 index 00000000..8126f069 --- /dev/null +++ b/servers/http/README.md @@ -0,0 +1,34 @@ +# 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 +npm start +``` + +## Test + +`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/index.js b/servers/http/index.js new file mode 100644 index 00000000..34fc8e58 --- /dev/null +++ b/servers/http/index.js @@ -0,0 +1,31 @@ +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, () => console.log('serveur work at http://localhost:3000')) + +export default server diff --git a/servers/http/package.json b/servers/http/package.json new file mode 100644 index 00000000..8c7bebdf --- /dev/null +++ b/servers/http/package.json @@ -0,0 +1,16 @@ +{ + "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" + }, + "engines": { + "node": "20" + } +} diff --git a/servers/http/test/http.test.js b/servers/http/test/http.test.js new file mode 100644 index 00000000..4f916dfd --- /dev/null +++ b/servers/http/test/http.test.js @@ -0,0 +1,32 @@ +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 { + // 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!' } + + 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()) +})