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 2 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
13 changes: 13 additions & 0 deletions servers/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Simple HTTP Server

## Usage

```bash
npm start
```

## Test

```bash
npm test
```
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions servers/http/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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)
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved

export default server
13 changes: 13 additions & 0 deletions servers/http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"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"
}
PatrickHeneise marked this conversation as resolved.
Show resolved Hide resolved
}
31 changes: 31 additions & 0 deletions servers/http/test/http.test.js
Original file line number Diff line number Diff line change
@@ -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')
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())
})