We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
This is a simple HTTP server. Not support HTTP request body.
import getopts from 'tjs:getopts'; import { addr } from './utils.js'; function parseRequestLine(requestLine) { const [method, path] = requestLine.split(' '); return { method, path }; } async function handleConnection(conn) { const buf = new Uint8Array(65536); let len = 0 while (true) { const nread = await conn.read(buf); len += (nread || 0); const requestData = new TextDecoder().decode(buf.subarray(0, len)); if (requestData.includes('\r\n\r\n')) { const [requestLine, ...headersLines] = requestData.split('\r\n'); const { method, path } = parseRequestLine(requestLine); const headers = Object.fromEntries(headersLines.map(line => line.split(': ', 2))); const request = `Method: ${method}\nPath: ${path}\nHeaders: ${JSON.stringify(headers, null, 2)}`; console.log('Received request:'); console.log(request); const response = `HTTP/1.1 200 OK\r\nContent - Type: text/html\r\n\r\n<html><body><pre>${request}</pre></body></html>`; conn.write((new TextEncoder().encode(response))); conn.close(); break; } } } const options = getopts(tjs.args.slice(2), { alias: { listen: 'l', port: 'p' }, default: { listen: '127.0.0.1', port: 1234 } }); const l = await tjs.listen('tcp', options.listen, options.port); console.log(`Listening on http://${addr(l.localAddress)}`); for await (let conn of l) { handleConnection(conn); conn = undefined; }
The text was updated successfully, but these errors were encountered:
Nice, thanks for sharing!
Sorry, something went wrong.
No branches or pull requests
This is a simple HTTP server. Not support HTTP request body.
The text was updated successfully, but these errors were encountered: