Skip to content

app-generator/api-server-nodejs-mongo

Repository files navigation

Simple starter built with Typescrypt / Express / MongoDB and JWT Auth powered by Passport library - provided and actively supported by AppSeed App Generator. The authentication flow is based on json web tokens and passport-jwt strategy - Inspired by other great OSS starters mentioned in the credits section.


Features:

  • API Definition - the unified API structure implemented by this server
  • ✅ Simple, intuitive codebase - built for beginners (can be extended with ease)
  • ✅ Node JS / Express / Typescript
  • Persistance: MongoDB
  • ✅ Auth: Passport / passport-jwt strategy
  • Docker

Can be used with other React Starters for a complete Full-Stack experience:

React Node JS Berry React Node Soft Dashboard React Node Horizon
React Node JS Berry React Node Soft Dashboard React Node Horizon

Nodejs API Server - Open-source Nodejs Starter provided by AppSeed.


Requirements


How to use the code

Create .env from .env.sample

Update the file with your data.

Clone the sources

$ git clone https://github.com/app-generator/api-server-nodejs-mongo.git
$ cd api-server-nodejs-mongo

Install dependencies via NPM or Yarn

$ npm i
// OR
$ yarn

Start the API server - development mode

$ npm dev
// OR
$ yarn dev

Start the API server - for production (files served from build/build/index.js)

$ npm start
// OR
$ yarn start

The API server will start using the PORT specified in .env file (default 5000)


Codebase Structure

< ROOT / src >
     | 
     |-- config/                              
     |    |-- config.ts             # Configuration       
     |    |-- passport.ts           # Define Passport Strategy             
     | 
     |-- models/                              
     |    |-- activeSession.ts      # Sessions Model (Mongo)              
     |    |-- user.ts               # User Model (Mongo) 
     | 
     |-- routes/                              
     |    |-- users.ts              # Define Users API Routes
     | 
     | 
     |-- index.js                     # API Entry Point
     |-- .env                       # Specify the ENV variables
     |                        
     |-- ************************************************************************

Mongo Settings

The Mongo URI lives in config/keys.js

... = 'mongodb:https://localhost/api_server_nodejs'

API

For a fast set up, use this POSTMAN file: api_sample

Register - api/users/signup

POST api/users/signup
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"
}

License

MIT @ AppSeed


Credits

This software is provided by the core AppSeed team with an inspiration from other great NodeJS starters:



Nodejs API Server Mongo - provided by AppSeed App Generator