Skip to content

Latest commit

 

History

History
132 lines (95 loc) · 4.25 KB

README.md

File metadata and controls

132 lines (95 loc) · 4.25 KB

NodeJs/Typescript API Boilerplate

Open Source Love GitHub issues GitHub stars GitHub forks GitHub license

To Contribute

Please feel free to contribute to this project. To contribute, please follow this instructions

Running the app

NOTE: These instructions require MongoDB and Node.js to be installed on your environment.

Clone the Repository

git clone https://github.com/ofuochi/node-typescript-boilerplate.git
cd node-typescript-boilerplate

Install Dependencies

npm install

Copy Files

Sample env File into a .env File

Copy the sample environment file to a new environment file that holds the sensitive settings of your application.

cp env.sample .env

Run the App

npm run start

Or run in development watch mode. This uses nodemon to watch for file changes.

npm run dev

DB Seeding

The first time the application runs, MongoDB is seeded with default Tenant and default Admin User.

  1. Default tenant name is Default (obvioulsy)
  2. Default admin login detail;
    • Username: admin
    • Password: 123qwe

Swagger API Documentation

Open the URL http://localhost:3000/api-docs to view the the swagger documentation of the endpoints.

This will contain all the endpoints you expose to the client. Once you add a new endpoint, this endpoint will automatically be added! How cool is that?😎. Concentrate on building the functionality and business logic of your application. Swagger will do the documentation for you!.

Since this is a multi-tenant application, to authenticate (sign-in or sign-up), you need to pass a tenant ID in the header so that the application will know which tenant you are referencing during authentication.

To get the tenant details call the "get tenants" endpoint. For example, to get the details of the Default tenant, I'll call the endpoint http://localhost:3000/api/v1/tenants/default. Good thing is, you can do this directly on Swagger!

Run Tests

Run test once

npm run test

Or re-run test on every file change (watch mode)

npm run test-watch

REST Services

The application exposes a few REST endpoints which require you to pass x-tenant-id header. First, call the tenant endpoint /api/v1/tenant to get all the available tenants. Use any of the tenant IDs as the value for x-tenant-id

  • HTTP GET /api/v1/tenants
  • HTTP GET /api/v1/tenants/:query
  • HTTP GET /api/v1/secured (Requires a valid x-auth-token header)

You can use the following code snippet to call the secured endpoint:

fetch("http://localhost:3000/api/v1/secure", {
	method: "GET",
	headers: {
		"Content-Type": "application/json",
		"x-tenant-id": "TENANT_ID",
		"x-auth-token": "SOME_VALID_CREDENTIAL"
	}
})
	.then(r => {
		if (r.status === 200) {
			r.json().then(j => console.log(j));
		} else {
			console.log("ERROR", r.status);
		}
	})
	.catch(e => console.log(e));

You can use the following code snippet to call the secured endpoint with an invalid x-auth-token header:

fetch("http://localhost:3000/api/v1/secure", {
	method: "GET",
	headers: {
		"Content-Type": "application/json",
		"x-tenant-id": "TENANT_ID",
		"x-auth-token": "SOME_WRONG_CREDENTIAL"
	}
})
	.then(r => {
		if (r.status === 200) {
			r.json().then(j => console.log(j));
		} else {
			console.log("ERROR", r.status);
		}
	})
	.catch(e => console.log(e));