Express server with MongoDB and authentication and authorisation
This is a codebase that can be used as a starting point for an API that provides endpoints for authentication.
/api/signup
– create a new user account/api/login
– log in to an existing account/api/logout
– log out
Additionally, two routes are provided that require authorisation:
/api/me
– allow logged-in user (with any role) to read and update their details/api/users
– allow a user with theadmin
role to see a list of all users (further routes can be added to implement full CRUD functionality)
See API docs for full details.
The following features and technologies are used:
- Yarn
- ES2015/ES6
- Eslint
- MVC implementation using Node, Express, Mongo and Mongoose
- Travis for CI testing
- JSON data returned in JSON API format
- PassportJS for authentication and Permission for authorisation
- APIDOC for documenting the API endpoints
- Jasmine and Supertest for testing the API endpoints
Follow the instructions here:
Install the NodeJS packages:
$ yarn
Follow the instructions here:
www.mongodb.com/download-center#community
API code is in the
api
directory.
In a terminal tab, start the MongoDB daemon:
$ mongod
In another terminal tab, you can use nodemon
:
$ yarn nodemon
To check if the API is running, browse to:
Ensure that the following environment variables are set in the deployed environment:
MONGODB_URI
– points to a valid MongoDB instanceAPP_SECRET
– a randomly-generated secret (see passwordsgenerator.net/)
To start the API:
$ yarn start
To run all tests:
$ yarn test
Check the
.eslint
file for the Eslint rules.
The Javascript code can be linted as follows:
$ yarn lint
And to auto-correct any errors that can be corrected:
$ yarn lint:fix
API tests are in the
spec
directory.
To run the API tests:
$ yarn jasmine
To generate the documentation for the API endpoints:
$ yarn apidoc
And to view the output, open the apidocs/index.html
file in your browser.
Database-related code is in the
db
directory.
To seed the Mongo database with sample data:
$ yarn db:seed
When developing locally, running the following command will seed the local database:
$ yarn db:seed:local