CLI Companion

  • Hugging Face CLI
    • login
    • whoami
    • repo create
    • upload
    • download
    • lfs-enable-largefiles
    • scan-cache
    • delete-cache
  • Hapi CLI
    • new
    • start
    • build
    • test
    • plugin create
    • route add
  • Cloudflared
    • tunnel
    • tunnel run
    • tunnel list
    • tunnel delete
    • access
    • access tcp
    • update

    The `hapi start` command within the Hapi CLI is used to initiate a Hapi.js server application. It typically points to the server's entry point file, allowing the application to be run and made accessible. While Hapi CLI itself is less commonly used for production deployments compared to direct `node index.js` or process managers, it can be useful for development and scaffolding purposes.

    Syntax

    bash
    hapi start [options] <server-file>

    - `server-file`: This is the path to your main Hapi.js server file (e.g., `index.js`, `server.js`). This file should contain the logic to create and start your Hapi server.

    Options (Commonly associated concepts, though Hapi CLI itself might not have extensive built-in `start` options)

    While the core Hapi CLI `start` command is quite basic, when used in real-world scenarios, you often combine it with standard Node.js practices or external process managers. The "options" listed below are more conceptual or relate to how you might augment the start process.

    - There are no widely documented `--options` specifically for `hapi start` itself in the way `npm start` might interpret flags. The primary input is the server file.

    - **Environmental Variables**: You will frequently use environment variables (e.g., `PORT`, `NODE_ENV`) to configure your Hapi application. These are set *before* the `hapi start` command.

    bash
    PORT=3000 NODE_ENV=development hapi start index.js

    Usage Examples

    #### 1. Basic Server Startup

    Assuming you have a Hapi.js server file named `index.js` in your project root that exports a server instance:

    **`index.js` example:**

    javascript
    'use strict';
    
    const Hapi = require('@hapi/hapi');
    
    const init = async () => {
        const server = Hapi.server({
            port: process.env.PORT || 3000,
            host: 'localhost'
        });
    
        server.route({
            method: 'GET',
            path: '/',
            handler: (request, h) => {
                return 'Hello Hapi!';
            }
        });
    
        await server.start();
        console.log(`Server running on ${server.info.uri}`);
        return server;
    };
    
    process.on('unhandledRejection', (err) => {
        console.log(err);
        process.exit(1);
    });
    
    init();

    **Command:**

    bash
    hapi start index.js

    **Explanation:** This command tells Hapi CLI to execute `index.js`. If `index.js` successfully creates and starts a Hapi server, you will see output similar to `Server running on http://localhost:3000` (or whatever port your server is configured for).

    #### 2. Starting with a Custom Port via Environment Variable

    You can specify the port your Hapi server listens on by setting the `PORT` environment variable before running the command, assuming your `index.js` is configured to read it (as in the example above).

    **Command:**

    bash
    PORT=8080 hapi start index.js

    **Explanation:** This starts the Hapi server defined in `index.js`, but this time it will listen on port `8080` instead of the default `3000` specified in the code, because the `PORT` environment variable overrides it.

    #### 3. Using `npm start` to wrap `hapi start`

    It's common practice to define a `start` script in your `package.json` to make it easier to run your application.

    **`package.json` example:**

    json
    {
      "name": "my-hapi-app",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "start": "hapi start index.js",
        "dev": "nodemon index.js" 
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "@hapi/hapi": "^21.3.2"
      }
    }

    **Command:**

    bash
    npm start

    **Explanation:** When you run `npm start`, npm executes the script defined under `"start"` in your `package.json`, which in this case is `hapi start index.js`. This provides a standardized way to start your application.

    Further Explanations

    - **Entry Point**: The `server-file` is crucial. It must be a Node.js module that, when executed, initializes and starts your Hapi server. If your server is not started within this file (e.g., if it only exports a configuration object), the `hapi start` command alone won't make your application live.

    - **Development vs. Production**: For development, `hapi start` is fine. For production, you might consider using process managers like PM2 or systemd services that offer features like automatic restarts, logging, and monitoring. These typically execute `node index.js` directly or use the `npm start` script.

    - **Hapi CLI's Role**: The Hapi CLI is more focused on project scaffolding (`hapi new`), plugin generation (`hapi plugin`), and potentially some development conveniences. The `start` command is a straightforward way to run your Hapi app without directly invoking `node`.

    - **Error Handling**: Ensure your `server-file` includes robust error handling for server startup failures (e.g., port already in use) and unhandled rejections/exceptions to prevent silent crashes.