Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

feat: add simple http server example #44

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions servers/http/README.md
Original file line number Diff line number Diff line change
@@ -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
```
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions servers/http/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import http from 'node:http'

const server = http.createServer()
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved

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({}))
}
})
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved

server.listen(3000, () => console.log('serveur work at http://localhost:3000'))

export default server
16 changes: 16 additions & 0 deletions servers/http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "node-example-http",
"version": "1.0.0",
"description": "A simple http server with Node.js",
"keywords": [],
"author": "Patrick Heneise <[email protected]>",
"type": "module",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "node --test"
},
"engines": {
"node": "20"
}
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved
}
32 changes: 32 additions & 0 deletions servers/http/test/http.test.js
Original file line number Diff line number Diff line change
@@ -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')
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved
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())
})