Getting Started with the RESTful API Node Starter Template
Tags: node, express, mongodb, api
Published: 2024-06-28 15:13:51

---
title: Getting Started with the RESTful API Node Starter Template
published: true
description: Creating a RESTful API from scratch can be a daunting task, especially when you need to ensure it's production-ready with all the essential features like authentication, validation, and logging. But what if you could kickstart your project with a robust, fully-configured template? Introducing the [Node.js Starter Template](https://github.com/mdashikar/starter-template-nodejs), a comprehensive boilerplate that simplifies API development. This template leverages Node.js, Express, and Mongoose to provide a solid foundation for your next project.

tags: Nodejs, ExpressJs, Mongodb, API
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-06-28 15:02 +0000
---


### Introduction

Creating a RESTful API from scratch can be a daunting task, especially when you need to ensure it's production-ready with all the essential features like authentication, validation, and logging. But what if you could kickstart your project with a robust, fully-configured template? Introducing the [Node.js Starter Template](https://github.com/mdashikar/starter-template-nodejs), a comprehensive boilerplate that simplifies API development. This template leverages Node.js, Express, and Mongoose to provide a solid foundation for your next project.

### Quick Start

Getting started with this template is incredibly easy. You can create a new project with just one command:

```bash
npx create-starter-template-nodejs <project-name>
```

Alternatively, you can use npm:

```bash
npm init starter-template-nodejs <project-name>
```

For those who prefer manual installation, follow these steps:

1. Clone the repository:

    ```bash
    git clone --depth 1 https://github.com/mdashikar/starter-template-nodejs.git
    cd starter-template-nodejs
    npx rimraf ./.git
    ```

2. Install the dependencies:

    ```bash
    npm install
    ```

3. Set the environment variables:

    ```bash
    cp .env.example .env
    ```

### Features

This starter template comes packed with features to jumpstart your development process:

- **MongoDB:** Use Mongoose for object data modeling.
- **Authentication & Authorization:** Implement JWT-based authentication and role-based access control.
- **Request Validation:** Utilize Joi for request data validation.
- **Comprehensive Logging:** Employ Winston and Morgan for effective logging.
- **API Documentation:** With swagger-jsdoc and swagger-ui-express
- **Process Management:** Use PM2 for advanced production process management.
- **Environment Configuration:** Manage environment variables with dotenv and cross-env.
- **Security Measures:** Enhance security with Helmet and sanitize inputs against XSS and query injection.
- **CORS & Compression:** Enable Cross-Origin Resource Sharing and gzip compression.
- **Docker Support:** Run your application in a Docker container.
- **Git Hooks:** Husky and lint-staged.
- **Linting & Formatting:** Maintain code quality with ESLint and Prettier.

### Running the Application

To run the application locally:

```bash
npm run dev
```

For production:

```bash
npm run start
```

### Testing

This template supports extensive testing with Jest:

- Run all tests:

    ```bash
    npm test
    ```

- Run tests in watch mode:

    ```bash
    npm test:watch
    ```

### Using Docker

You can also run the application in a Docker container:

- For development:

    ```bash
    npm run docker:dev
    ```

- For production:

    ```bash
    npm run docker:prod
    ```

- To run tests in a Docker container:

    ```bash
    npm run docker:test
    ```

### Linting and Formatting

Ensure your code adheres to best practices:

- Run ESLint:

    ```bash
    npm run lint
    ```

- Fix ESLint errors:

    ```bash
    npm run lint:fix
    ```

- Run Prettier:

    ```bash
    npm run prettier
    ```

- Fix Prettier errors:

    ```bash
    npm run prettier:fix
    ```

### Environment Variables

Modify the environment variables in the `.env` file to fit your setup. Here are some defaults:

```bash
NODE_ENV=development
PORT=3000
MONGODB_URL=mongodb://mongodb:27017/node-boilerplate
JWT_SECRET=thisisasamplesecret
JWT_ACCESS_EXPIRATION_MINUTES=30
JWT_REFRESH_EXPIRATION_DAYS=30
SMTP_HOST=smtp-relay.brevo.com
SMTP_PORT=587
SMTP_AUTH_USER=601f7f001@smtp-brevo.com
SMTP_AUTH_PASSWORD=BRGVQs6vacTC37d5
MAIL_FORM=support@starter-template-nodejs.com
```

### Conclusion

The RESTful API Node Starter Template is a powerful tool for quickly building RESTful APIs using Node.js, Express, and Mongoose. With its rich set of features and well-organized project structure, it provides a solid foundation for your next project.

Ready to start building? Check out the [GitHub repository](https://github.com/mdashikar/starter-template-nodejs) and get started today!

Happy coding!