> ## Documentation Index
> Fetch the complete documentation index at: https://magicserver.arsh.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js

> How to add automatic HTTPS to any Node.js application with MagicServer

<Info>
  **Prerequisites**:

  * A recent version of Node.js (22 or higher)
  * A package manager like npm, pnpm or yarn
</Info>

<Steps>
  <Step title="Setup DNS Records">
    Before we can start with any code, we need to ensure that the application's domain is pointing to the server.

    To do this, go to your cloud provider's dashboard and find the IP address of the machine that will run your application.
    Then, from your DNS provider's dashboard, create an "A" record with the value set to the IP address of the server.
  </Step>

  <Step title="Install MagicServer">
    <Tabs>
      <Tab title="npm">
        ```bash theme={null}
        npm install @magicserver/magicserver
        ```
      </Tab>

      <Tab title="pnpm">
        ```bash theme={null}
        pnpm install @magicserver/magicserver
        ```
      </Tab>

      <Tab title="bun">
        ```bash theme={null}
        bun install @magicserver/magicserver
        ```
      </Tab>

      <Tab title="yarn">
        ```bash theme={null}
        yarn install @magicserver/magicserver
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Replace you usage of `createServer` with `createMagicServer`">
    ```js server.js theme={null}
    import { createServer } from "node:http"   // [!code --]
    import { createMagicServer } from "@magicserver/magicserver"  // [!code ++]

    function app(req, res) {
      res.end("Hello, World!")
    }

    const server = createServer(app);  // [!code --]
    const server = createMagicServer("your-domain.com", app);  // [!code ++]

    server.listen(80) // [!code --]
    ```

    Notice that we also removed the `server.listen()` call.
    We do this because MagicServer will automatically listen on port 443, which is the port for HTTPS.
  </Step>

  <Step title="Start the server">
    Now whenever your application is started, it will be automatically served over HTTPS and stay up to date with certificates without needing any intervention.

    ```bash theme={null}
    node server.js
    ```
  </Step>
</Steps>

<br />

<Card title="Curious about how it works?" icon="rocket" horizontal href="/docs/how-it-works">
  It’s not a sleight of hand! Learn the magic behind MagicServer.
</Card>
