vanshul22 / Node-js-Initial-template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node Js Starter Template API with MySQL, Express and JWT Authentication.

Introduction

This project provides a RESTful API for user management. It allows users to register, login, view their profile and update their information. The API is built using Node.js, Express and MySQL. It utilizes JSON Web Tokens (JWT) for authentication.

Prerequisites

To run this project, you will need the following:

  • Node.js installed on your system.
  • MySQL database server running.
  • A MySQL client, such as MySQL Workbench, to create and manage the database.

Setup

  1. Clone the project repository.
git https://github.com/vanshul22/Node-js-Initial-template.git
  1. Install the dependencies.
npm install
  1. Create a .env file in the project root directory and add the following environment variables:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=test
JWT_SECRET=secret
  1. Create the database and tables.
mysql -u root -p password -D test < schema.sql
  1. Server :

(A) Start the Production server.

npm run start

(B) Start the Development Server.

npm run dev

Usage

Register a new user

curl -X POST http://localhost:3000/api/users -H "Content-Type: application/json" -d '{"username": "johndoe", "password": "secret"}'

Login

curl -X POST http://localhost:3000/api/users/login -H "Content-Type: application/json" -d '{"username": "johndoe", "password": "secret"}'

Get all users

curl -X GET http://localhost:3000/api/users -H "Authorization: Bearer <token>" -H "Content-Type: application/json"

Get a user by ID

curl -X GET http://localhost:3000/api/users/1 -H "Authorization: Bearer <token>" -H "Content-Type: application/json"

Update a user

curl -X PUT http://localhost:3000/api/users/1 -H "Authorization: Bearer <token>" -H "Content-Type: application/json"

Delete a user

curl -X DELETE http://localhost:3000/api/users/1 -H "Authorization: Bearer <token>" -H "Content-Type: application/json"

About


Languages

Language:JavaScript 100.0%