This repository was archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
feat: add simple http server example #44
Open
PatrickHeneise
wants to merge
5
commits into
nodejs:main
Choose a base branch
from
PatrickHeneise:feat/simple-http-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20aefb5
feat: add simple http server example
PatrickHeneise dddc200
chore: sort package json
PatrickHeneise c5ede85
Apply suggestions from code review
PatrickHeneise da749f5
chore: README update
PatrickHeneise e905126
fix: add links to mdn and node docs
PatrickHeneise File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.