ℹ️User guide: node-mongo setup, installation and usage

NODE-MONGO USER DOCUMENTATION


Build backend applications for your projects with node-mongo. This is a quick setup, installation, and usage guide. On this page, you will learn how to quickly create a new backend API using node-mongo. Follow hints that give instructions on where to find more robust content on the node-mongo features and commands, or you can just simply check the sidebar.


  • Operating System Requirements: Works on Mac, Windows, and Linux Operating Systems.

  • Installation Requirements: To be able to use node-mongo, you need Node js to be installed on your computer. You can get Node js from the official Node js website, or install Node js through the Node Version Manager (NVM).

  • Supported Node.js versions: Node js versions 16.x - 20.x


Install node-mongo CLI

Install CLI globally using this command:

npm install -g @code-collabo/node-mongo-cli

Create a new backend API application

Change directory i.e. cd into any folder of choice on your computer. Then run the node-mongo command together with test-folder and ts as shown in the code block below. This will generate a new backend API called test-folder, and the code in the application generated will be in typescript (ts).

node-mongo test-folder ts

See Reference Guide: node-mongo features and commands for more details on other types of templates you can generate apart from the typescript (ts) template. You can find this in the sidebar.


Running the generated backend API application

Change directory i.e. cd into the test-folder application generated earlier. Then follow the steps below depending on the MongoDB connection or setup type that you wish to use.

Step 1

Install dependencies:

npm install

Step 2

  • Ensure you have an internet connection

  • Have a monogDB atlas cluster set up in the cloud

  • Get your Atlas mongoDB URI string

Step 3

  • Rename the .env.example file to .env

  • Change PORT_ATLAS environment variable to your preferred port number in the .env file

  • Add your Atlas mongoDB URI string to the MONGODB_ATLAS_URI environment variable in the .env file

Step 4

Start the automated development server and choose ATLAS connection:

npm run dev

Step 4 (alternative)

You can also use the (manual) development server alternative for connection to MongoDB atlas:

npm run dev:atlas

npm run dev starts the automated development server. It prompts you to choose your preferred connection setup type the first time you use it and saves the chosen connection setup type for every other time you come back to use it. It also automatically installs or sets up the DB and server files for the chosen connection setup type.

npm run dev:restore resets the automated development server back to first-time usage condition i.e. it removes your previously saved connection setup type and the development server will now assume that you are a first-timer. After using this command, you will now have the option to set your preferred connection type again the next time you start the server with the npm run dev command.

npm run dev:change is useful for when you are not a first-time user and want to change your connection setup type without restoring the automated development server to first-time usage condition. It will prompt you to choose your connection setup type, but it will not install the DB and server files for the chosen connection setup type.


API design

A "demo" setup exists in the generated backend API application i.e. demo database collection and schema, /demo endpoints, etc. that you can test with. Find more details about the available test endpoints that you can try out in the table below:

Help needed (content not available yet): Contribute to this doc. Help users know how they can add their own db collections, schemas, endpoints, files, etc. to the generated backend API application.


API call requests and responses

GET /demo

Request body shape

No request body

Successful response shape

{
    "count": number,
    "items": [
        {
            "_id": "string",
            "name": "string",
            "age": number,
            "request": {
                "type": "string",
                "url": "string"
            }
        },
        // etc.
    ]
}
POST /demo

Request body shape

{
    "name": "string",
    "age": number
}

Successful response shape

{
    "message": "string",
    "newItem": {
        "_id": "string",
        "name": "string",
        "age": number,
        "request": {
            "type": "string",
            "url": "string"
        }
    }
}
GET /demo/:demoId

Request body shape

No request body

Successful response shape

{
    "_id": "string",
    "name": "string",
    "age": number,
    "request": {
        "type": "string",
        "description": "string",
        "url": "string"
    }
}
PATCH /demo/:demoId

Request body shape

[
    { "propName": "string", "value": "string" }
]

OR

[
    { "propName": "string", "value": number }
]

i.e. propName can be string "name" or "age". Value is a string when name is the propName, while value is a number when age is the propName.

Successful response shape

{
    "message": "string",
    "request": {
        "type": "string",
        "description": "string",
        "url": "string"
    }
}
PUT /demo/:id

Request body shape

{
    "name": "string",
    "age": number
}

Successful response shape

{
    "message": "string",
    "request": {
        "type": "string",
        "description": "string",
        "url": "string"
    }
}
DELETE /demo/:demoId

Request body shape

No request body

Successful response shape

{
    "message": "string",
    "request": {
        "type": "string",
        "description": "string",
        "url": "string",
        "body": {
            "name": "string",
            "age": "string"
        }
    }
}

Help needed (content not available yet): Contribute to this doc and node-mongo API. Help create swagger documentation for testing API call requests and responses, and help link to this doc. Instead of what we have here now.

Last updated