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, 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:
npx create-starter-template-nodejs <project-name>
Alternatively, you can use npm:
npm init starter-template-nodejs <project-name>
For those who prefer manual installation, follow these steps:
-
Clone the repository:
git clone --depth 1 https://github.com/mdashikar/starter-template-nodejs.git cd starter-template-nodejs npx rimraf ./.git
-
Install the dependencies:
npm install
-
Set the environment variables:
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:
npm run dev
For production:
npm run start
Testing
This template supports extensive testing with Jest:
-
Run all tests:
npm test
-
Run tests in watch mode:
npm test:watch
Using Docker
You can also run the application in a Docker container:
-
For development:
npm run docker:dev
-
For production:
npm run docker:prod
-
To run tests in a Docker container:
npm run docker:test
Linting and Formatting
Ensure your code adheres to best practices:
-
Run ESLint:
npm run lint
-
Fix ESLint errors:
npm run lint:fix
-
Run Prettier:
npm run prettier
-
Fix Prettier errors:
npm run prettier:fix
Environment Variables
Modify the environment variables in the .env
file to fit your setup. Here are some defaults:
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 and get started today!
Happy coding!
Top comments (0)