npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backend-template-martinherranzc

v1.0.21

Published

Hexagonal Template for NodeJs Backend/Fullstack projects with Swagger API Documentation

Downloads

1,697

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.