Express / Nodejs Starter with JWT authentication, OAuth (Github), and SQLite persistance - Provided by AppSeed
. Authentication Flow uses json web tokens
via Passport library - passport-jwt
strategy.
Features:
- ๐
Full-stack ready
with React Soft Dashboard - โ Simple, intuitive codebase - can be extended with ease.
- โ
TypeScript
,Joy
for validation - โ Stack: NodeJS / Express / SQLite / TypeORM
- โ
Auth: Passport /
passport-jwt
strategy - ๐
OAuth
for Github
Tested with:
NodeJS | NPM | YARN |
---|---|---|
v18.0.0 |
โ | โ |
v16.10.0 |
โ | โ |
v14.15.0 |
โ | โ |
๐ Step 1 - Clone the project
$ git clone https://github.com/app-generator/api-server-nodejs.git
$ cd api-server-nodejs
๐ Step 2 - Install dependencies via
Yarn
$ npm i
// OR
$ yarn
๐ Step 3 - Run the SQLite migration via TypeORM
$ npm run typeorm migration:run
// OR
$ yarn typeorm migration:run
๐ Step 4 - Edit the
.env
using the template.env.sample
.
PORT=5000 # API PORT
SQLITE_PATH=./database.db # Path to the SQLite database file
SECRET="Whatever-STRONG" # Secret for sensitive data hashing
# Same as for React APP
GITHUB_OAUTH_CLIENT_ID= ... # Github OAuth secret
GITHUB_OAUTH_CLIENT_SECRET= ... # Github OAuth secret
๐ Step 5 - Start the API server (development mode)
$ npm run dev
// OR
$ yarn dev
๐ Step 6 - Production Build (files generated in
build
directory)
$ yarn build
๐ Step 7 - Start the API server for production (files served from
build/index.js
)
$ yarn start
The API server will start using the PORT
specified in .env
file (default 5000).
< ROOT / src >
|
|-- config/
| |-- config.ts # Configuration
| |-- passport.ts # Define Passport Strategy
|
|-- migration/
| |-- some_migration.ts # database migrations
|
|-- models/
| |-- activeSession.ts # Sessions Model (Typeorm)
| |-- user.ts # User Model (Typeorm)
|
|-- routes/
| |-- users.ts # Define Users API Routes
|
|
|-- index.js # API Entry Point
|-- .env # Specify the ENV variables
|
|-- ************************************************************************
The SQLite Path is set in .env
, as SQLITE_PATH
๐ Generate migration:
$ yarn typeorm migration:generate -n your_migration_name
๐ Run migration:
$ yarn typeorm migration:run
For a fast set up, use this POSTMAN file: api_sample
๐ Register -
api/users/register
POST api/users/register
Content-Type: application/json
{
"username":"test",
"password":"pass",
"email":"[email protected]"
}
๐ Login -
api/users/login
POST /api/users/login
Content-Type: application/json
{
"password":"pass",
"email":"[email protected]"
}
๐ Logout -
api/users/logout
POST api/users/logout
Content-Type: application/json
authorization: JWT_TOKEN (returned by Login request)
{
"token":"JWT_TOKEN"
}
๐ Using npm:
$ npm run update-role [user_id] [role_id (optional)]
๐ Using yarn:
$ yarn update-role [user_id] [role_id (optional)]
- [user_id] is the id of existing user to update role for.
- [role_id] is the id of role: 1 for admin & 2 for user. If you don't provide any role_id it would update user to admin role.
$ npm run test
// OR
$ yarn test
This software is provided by the core AppSeed team with an inspiration from other great NodeJS starters:
- Initial verison - coded by Teo Deleanu
- Hackathon Starter - A truly amazing boilerplate for Node.js apps
- Node Server Boilerplate - just another cool starter
- React NodeJS Argon - released by Creative-Tim and ProjectData
Node JS API Server - provided by AppSeed App Generator