<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://freemwiki.com/index.php?action=history&amp;feed=atom&amp;title=Openai%2F691ddcec-579c-8003-b973-cdb692b1d5db</id>
	<title>Openai/691ddcec-579c-8003-b973-cdb692b1d5db - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://freemwiki.com/index.php?action=history&amp;feed=atom&amp;title=Openai%2F691ddcec-579c-8003-b973-cdb692b1d5db"/>
	<link rel="alternate" type="text/html" href="https://freemwiki.com/index.php?title=Openai/691ddcec-579c-8003-b973-cdb692b1d5db&amp;action=history"/>
	<updated>2026-04-09T20:48:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://freemwiki.com/index.php?title=Openai/691ddcec-579c-8003-b973-cdb692b1d5db&amp;diff=593472&amp;oldid=prev</id>
		<title>Lukegao: by freem.ai...</title>
		<link rel="alternate" type="text/html" href="https://freemwiki.com/index.php?title=Openai/691ddcec-579c-8003-b973-cdb692b1d5db&amp;diff=593472&amp;oldid=prev"/>
		<updated>2025-11-20T05:39:03Z</updated>

		<summary type="html">&lt;p&gt;by freem.ai...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
{{DISPLAYTITLE:Host static site Node.js}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;This conversation was exported from ChatGPT&amp;#039;&amp;#039;&amp;lt;ref&amp;gt;{{cite web|title=Host static site Node.js|url=https://chatgpt.com/share/691ddcec-579c-8003-b973-cdb692b1d5db|publisher=ChatGPT|access-date=2025-11-20}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== gpt-5-1 ==&lt;br /&gt;
=== User: How do i host a static website in node.js, but where the first folder is taken from the host name. ===&lt;br /&gt;
How do i host a static website in node.js, but where the first folder is taken from the host name. &lt;br /&gt;
&lt;br /&gt;
So if people go to banana.com to my node.js app, it should serve the banana.com folder. And if nothing can be found a 404 need to be returned.&lt;br /&gt;
&lt;br /&gt;
=== Assistant: Below is a simple and clean way to do this in Node.js + Express: ===&lt;br /&gt;
Below is a simple and clean way to do this in Node.js + Express:&lt;br /&gt;
&lt;br /&gt;
===== ✅ Goal =====&lt;br /&gt;
&lt;br /&gt;
Map each hostname to a folder with the same name, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;banana.com   → ./sites/banana.com/&lt;br /&gt;
apple.com    → ./sites/apple.com/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the file doesn’t exist → return 404.&lt;br /&gt;
&lt;br /&gt;
=== ✔️ Working Example (Express) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;const express = require(&amp;quot;express&amp;quot;);&lt;br /&gt;
const path = require(&amp;quot;path&amp;quot;);&lt;br /&gt;
const fs = require(&amp;quot;fs&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const app = express();&lt;br /&gt;
&lt;br /&gt;
// all hosts served from ./sites/&amp;lt;hostname&amp;gt;/&lt;br /&gt;
const SITES_ROOT = path.join(__dirname, &amp;quot;sites&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
app.use((req, res, next) =&amp;gt; {&lt;br /&gt;
  const host = req.hostname; // e.g. &amp;quot;banana.com&amp;quot;&lt;br /&gt;
  const siteDir = path.join(SITES_ROOT, host);&lt;br /&gt;
&lt;br /&gt;
  // Check if the site folder exists&lt;br /&gt;
  if (!fs.existsSync(siteDir)) {&lt;br /&gt;
    return res.status(404).send(&amp;quot;Site not found&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // Create a static handler for this request&lt;br /&gt;
  express.static(siteDir)(req, res, (err) =&amp;gt; {&lt;br /&gt;
    if (err) return next(err);&lt;br /&gt;
    res.status(404).send(&amp;quot;File not found&amp;quot;);&lt;br /&gt;
  });&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
const PORT = process.env.PORT || 3000;&lt;br /&gt;
app.listen(PORT, () =&amp;gt; console.log(&amp;lt;code&amp;gt;Listening on ${PORT}&amp;lt;/code&amp;gt;));&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ✔️ Folder structure ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;your-app/&lt;br /&gt;
  server.js&lt;br /&gt;
  sites/&lt;br /&gt;
    banana.com/&lt;br /&gt;
      index.html&lt;br /&gt;
    apple.com/&lt;br /&gt;
      index.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ✔️ How it works ===&lt;br /&gt;
# Every request hits a middleware.&lt;br /&gt;
# We inspect req.hostname to know which folder to serve.&lt;br /&gt;
# We check if ./sites/&amp;lt;hostname&amp;gt; exists.&lt;br /&gt;
# We attach a static-file handler dynamically to serve from that folder.&lt;br /&gt;
# If no file matches → 404.&lt;br /&gt;
&lt;br /&gt;
=== ✔️ Notes / Tips ===&lt;br /&gt;
&lt;br /&gt;
===== If using reverse proxies (Nginx, Cloudflare, etc.) =====&lt;br /&gt;
&lt;br /&gt;
Make sure they pass the correct Host header.&lt;br /&gt;
&lt;br /&gt;
===== Wildcard domains (e.g., *.example.com)? =====&lt;br /&gt;
&lt;br /&gt;
You can adapt it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;js&amp;quot;&amp;gt;const host = req.hostname.split(&amp;#039;:&amp;#039;)[0];&lt;br /&gt;
const sub = host.split(&amp;#039;.&amp;#039;)[0];&lt;br /&gt;
const siteDir = path.join(SITES_ROOT, sub);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want, I can help you:&lt;br /&gt;
✔ create a version using only Node (no Express)&lt;br /&gt;
✔ support for default domain fallback&lt;br /&gt;
✔ caching static handlers for performance&lt;br /&gt;
✔ support for multi-level domain matching&lt;br /&gt;
&lt;br /&gt;
Just tell me!&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:ChatGPT Shares]]&lt;br /&gt;
[[Category:openai]]&lt;/div&gt;</summary>
		<author><name>Lukegao</name></author>
	</entry>
</feed>