basic node REST API with express & mongodb.
- basic CRUD REST API (book catalog)
- basic token-based authentication REST API (register & login, token-protected endpoints)
- persistence with mongodb via
mongoose
- hashed passwords with
bcrypt
- token issuance and verification with
jsonwebtokens
(jwt) - (todo) caching some endpoints with
redis
- (todo) add mongodb auth
- (todo) javascript style docstrings
- (todo) dockerized deployment
npm
+node
docker
and (optionally)docker-compose
for runningmongodb
or else a localmongodb
install- (optional)
python
+jupyter
for testing the api, else use Postman or VS Code "REST Client" extension
# start mongodb (from ./docker/dev)
docker-compose up -d
# install necessary packages
npm install
# run with nodemon
npm start
you will need a .env
file like below for testing:
MONGODB_URL=mongodb://localhost:27017/<db_name>
TOKEN_SECRET=<some random string>
REDIS_PORT=6379
API_PORT=<port_to_run_on, default 5000>
for development, you can access mongodb
and redis
like:
# start mongo shell from docker-compose mongo
docker exec -it dev_devmongo_1 mongo
# start redis-cli from docker-compose redis
docker exec -it dev_devredis_1 redis-cli
see the included test-xxx.ipynb
in /tests
for testing with python notebook, or see the test.rest
file for testing with VS Code extension.
post new user
Request:
{
name: string, required
username: string, required
email: string, required
password: string, required
}
post
Request:
{
username: string, required
password: string, required
}
hello world test
hello world test, with authentication
you should get response with {"hello": <username> }
if you send your JWT token as header (after registering, see below)
return list of all books in the db
get a book by its id
post book to db
Request:
{
title: string, required
author: string, required
genre: string
isbn: string
publisher: string
description: string
}
update a book in the db. only send the fields to update.
{
title: string
author: string
genre: string
isbn: string
publisher: string
description: string
}
remove a book from the database
search for book(s) with the associated key-value pair. e.g. ?genre=fantasy
or ?author=Robert Jordan
Build A Restful Api With Node.js Express & MongoDB | Rest Api Tutorial
Build A Node.js API Authentication With JWT Tutorial
Build A REST API With Node.js, Express, & MongoDB - Quick
Node.js Crash Course Tutorial #9 - MongoDB
Redis Caching in Node.js