Skip to content

Commit 7c59c87

Browse files
authored
Merge pull request #37 from MDA2AV/fix/node
Fix node errors
2 parents 97487f2 + 6ac5b75 commit 7c59c87

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

docs/content/servers/node.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ const http = require('http');
2323
const port = parseInt(process.argv[2] || '8080', 10);
2424

2525
const server = http.createServer((req, res) => {
26-
const url = new URL(req.url, `http://${req.headers.host}`);
27-
if (url.pathname === '/echo') {
26+
let pathname;
27+
try {
28+
pathname = new URL(req.url, `http://${req.headers.host || 'localhost'}`).pathname;
29+
} catch {
30+
pathname = req.url;
31+
}
32+
if (pathname === '/echo') {
2833
let body = '';
2934
for (const [name, value] of Object.entries(req.headers)) {
3035
if (Array.isArray(value)) value.forEach(v => body += name + ': ' + v + '\n');

src/Servers/NodeServer/server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ const http = require('http');
33
const port = parseInt(process.argv[2] || '8080', 10);
44

55
const server = http.createServer((req, res) => {
6-
const url = new URL(req.url, `http://${req.headers.host}`);
7-
if (url.pathname === '/echo') {
6+
let pathname;
7+
try {
8+
pathname = new URL(req.url, `http://${req.headers.host || 'localhost'}`).pathname;
9+
} catch {
10+
pathname = req.url;
11+
}
12+
if (pathname === '/echo') {
813
let body = '';
914
for (const [name, value] of Object.entries(req.headers)) {
1015
if (Array.isArray(value)) value.forEach(v => body += name + ': ' + v + '\n');

0 commit comments

Comments
 (0)