Skip to content

Commit

Permalink
Move built-in HTTP pages to separeted file
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 1, 2024
1 parent c817819 commit 4baab15
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
24 changes: 24 additions & 0 deletions add-http-pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'

let hello
async function readHello() {
if (!hello) {
hello = await readFile(join(import.meta.dirname, '..', 'hello.html'))

Check failure on line 7 in add-http-pages/index.js

View workflow job for this annotation

GitHub Actions / Node.js Latest Full

Unhandled error

Error: ENOENT: no such file or directory, open '/home/runner/work/server/server/hello.html' ❯ open node:internal/fs/promises:638:25 ❯ Proxy.readFile node:internal/fs/promises:1238:14 ❯ readHello add-http-pages/index.js:7:13 ❯ add-http-pages/index.js:15:18 ❯ BaseServer.processHttp base-server/index.js:774:7 ❯ Server.<anonymous> base-server/index.js:614:9 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -2, code: 'ENOENT', syscall: 'open', path: '/home/runner/work/server/server/hello.html' } This error originated in "base-server/index.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "has hello page". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 7 in add-http-pages/index.js

View workflow job for this annotation

GitHub Actions / Node.js 20 Quick

Unhandled error

Error: ENOENT: no such file or directory, open '/home/runner/work/server/server/hello.html' ❯ open node:internal/fs/promises:639:25 ❯ Proxy.readFile node:internal/fs/promises:1242:14 ❯ readHello add-http-pages/index.js:7:13 ❯ add-http-pages/index.js:15:18 ❯ BaseServer.processHttp base-server/index.js:774:7 ❯ Server.<anonymous> base-server/index.js:614:9 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -2, code: 'ENOENT', syscall: 'open', path: '/home/runner/work/server/server/hello.html' } This error originated in "base-server/index.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "has hello page". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
}
return hello
}

export function addHttpPages(server) {
if (!server.options.disableHttpServer) {
server.http('GET', '/', async (req, res) => {
let data = await readHello()
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(data)
})
server.http('GET', '/health', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Logux Server: OK\n')
})
}
}
28 changes: 4 additions & 24 deletions base-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { LoguxNotFoundError } from '@logux/actions'
import { Log, MemoryStore, parseId, ServerConnection } from '@logux/core'
import { createNanoEvents } from 'nanoevents'
import { nanoid } from 'nanoid'
import { promises as fs } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import UrlPattern from 'url-pattern'
import { WebSocketServer } from 'ws'

import { addHttpPages } from '../add-http-pages/index.js'
import { Context } from '../context/index.js'
import { createHttpServer } from '../create-http-server/index.js'
import { ServerClient } from '../server-client/index.js'
Expand All @@ -22,17 +23,6 @@ function optionError(msg) {
throw error
}

let helloCache
async function readHello() {
if (!helloCache) {
let hello = await fs.readFile(
join(fileURLToPath(import.meta.url), '..', 'hello.html')
)
helloCache = hello.toString()
}
return helloCache
}

export async function wasNot403(cb) {
try {
await cb()
Expand Down Expand Up @@ -331,17 +321,7 @@ export class BaseServer {

this.httpListeners = {}
this.httpAllListeners = []
if (!this.options.disableHttpServer) {
this.http('GET', '/', async (req, res) => {
let hello = await readHello()
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(hello)
})
this.http('GET', '/health', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Logux Server: OK\n')
})
}
addHttpPages(this)

this.listenNotes = {}

Expand Down Expand Up @@ -638,7 +618,7 @@ export class BaseServer {
}

let pkg = JSON.parse(
await fs.readFile(
await readFile(
join(fileURLToPath(import.meta.url), '..', '..', 'package.json')
)
)
Expand Down

0 comments on commit 4baab15

Please sign in to comment.