Example: Static Site

The simplest way to use SpaceNode — serve a static website with HTML, CSS, JavaScript, and images. No modules, no controllers. Just files.

Project Structure

my-site/
├── app.js
├── package.json
└── public/
    ├── index.html
    ├── about.html
    ├── style.css
    └── img/
        └── logo.png

app.js

import { createApp } from 'spacenode'

const app = await createApp({
  static: './public',   // serve files from ./public
  spa: false,            // multi-page mode (each .html is its own page)
})

app.listen(3000, () => {
  console.log('Site running on http://localhost:3000')
})

That's it. 3 lines of code and your site is live.

package.json

{
  "type": "module",
  "dependencies": {
    "spacenode": "^1.0.0"
  },
  "scripts": {
    "start": "node app.js"
  }
}

public/index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Site</title>
  <link rel="stylesheet" href="/style.css">
</head>
<body>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
  <h1>Welcome!</h1>
  <p>This is a static site served by SpaceNode.</p>
  <img src="/img/logo.png" alt="Logo">
</body>
</html>

public/about.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>About — My Site</title>
  <link rel="stylesheet" href="/style.css">
</head>
<body>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
  <h1>About Us</h1>
  <p>A simple static page.</p>
</body>
</html>

How It Works

URLFile Served
/public/index.html
/aboutpublic/about.html (clean URL)
/about.htmlpublic/about.html
/style.csspublic/style.css
/img/logo.pngpublic/img/logo.png

What You Get

Tip — Need SPA mode instead? Set spa: true (or just remove the option — it's the default). Then all unmatched routes fall back to index.html, perfect for React/Vue/Svelte apps.
Production — For production, consider adding the compress and security pipes: pipe: ['compress', 'security']. This adds gzip/brotli compression and security headers (HSTS, X-Frame-Options, etc.).