backend-template-martinherranzc
v1.0.21
Published
Hexagonal Template for NodeJs Backend/Fullstack projects with Swagger API Documentation
Downloads
1,697
Maintainers
Readme
backend-template-martinherranzc
Description
Hello world! I'm Claudio Martin Herranz
and here i present you a template that can help developing for newcomers. It's my first package so i would love comments and suggestions.
This project provides a well-structured Node.js backend template employing Express and JSON Web Tokens (JWT) for authentication. It offers a solid foundation for building secure and scalable RESTful APIs.
Features
- Express Framework: Utilizes Express for efficient routing, middleware handling, and server management.
- JWT Authentication: Implements JWT for secure user authentication and authorization.
- Hexagonal Architecture: Implements hexagonal architecture to promote separation of concerns and enhance testability.
- Modular Structure: Encourages organized code with potential separation for controllers, models, and services (depending on project complexity).
- Testing: Supports unit testing with Vitest (or your preferred testing framework).
- Swagger: Documents your API.
Getting Started
Prerequisites
Node.js (version 14 or higher) and npm (or yarn) installed. Verify with:
node -v npm -v # or yarn -v
Installation
To install the package, use the following command:
npm install backend-template-martinherranzc
node node_modules/backend-template-martinherranzc/copy-structure.js
npm install
Usage
Once installed, you can start using the backend template in your project. Configure your application as needed, following the structure and features provided in this template.
Environment Variables
Create a .env
file in the project root. This file holds sensitive information, such as your JWT secret key. You can use a library like dotenv
to access these variables. Example:
JWT_SECRET=your_strong_jwt_secret_key
# Other environment variables (e.g., database connection details)
Important: Do not commit your
.env
file to version control. Configure your IDE to ignore this file.
Start the Server
npm start # or yarn start
This will start the server, typically listening on port 3000 (default for Express). Check your terminal logs to confirm the server is running.
Usage (Example)
User Registration
- Create an API endpoint (usually a POST request) to handle user registration.
- Upon successful registration, generate a JWT token using the
jsonwebtoken
library and the secret key from your.env
file. - Send the JWT token back to the client in the response.
User Login
- Create a login endpoint.
- Validate user credentials against your data store (e.g., a database).
- If valid, generate a JWT token as in registration.
Protected Endpoints
- Use middleware (e.g.,
express-jwt
) to protect routes that require authentication. - Decode the JWT token from the client's request headers to verify user identity and authorization.
Running Tests
Test Configuration
Refer to the Vitest documentation for setup and test writing guidelines. Place test files in a __tests__
directory and use Vitest syntax.
Run Tests
npm test # or yarn test
Development
Feel free to customize the project structure and add features as needed. Consider adding error handling, logging, and database integration for a fully functional backend API.
License
This project is licensed under the ISC License. See the LICENSE file for details.
Additional Notes
- Replace
your_strong_jwt_secret_key
in the.env
example with a long, random, and cryptographically secure key for production use. - Securely store your JWT secret key and other sensitive information.
- Consider using environment variables for sensitive configuration details.
- This template is adaptable, so explore best practices for secure and robust RESTful APIs with Node.js and Express.