Building out a REST API with Node, Express, MongoDB (Mongoose) and TypeScript.
Credit: TomDoesTech
yarn add express zod config cors mongoose pino pino-pretty dayjs bcrypt jsonwebtoken lodash nanoid
yarn add @types/body-parser @types/config @types/cors @types/express @types/node @types/pino @types/bcrypt @types/jsonwebtoken @types/lodash @types/nanoid ts-node-dev typescript -D
To be able to interact with the database, we need one or a combination of some of the products in the MongoDB ecosystem
- Shell -
mongosh
newer CLI tool - Compass - Desktop GUI
- Atlas - Cloud GUI
- CLI for Cloud -
mongocli
to manage Atlas - Connector for BI - further exploration for data visualisation, connect with Power BI
This is deprecated since version 5.0.
- use homebrew or chocolatey to install MongoDB
- alternatively download and install MongoDB msi
- use bash to run
mongo
show dbs
to list databases- pick one to use with
use <db>
- download MongoDB Shell
- To use the MongoDB Shell, you must have a MongoDB deployment to connect to - docs.mongodb
- connect to a MongoDB Atlas cluster / mongoose + express instance
- use bash to run
mongosh
- login to Atlas
- create a Cluster, and within that a Database
- create a db user + password (store in
.env
as variables) - hit connect > add IP / from anywhere > copy the connection device string to use as dbUri
Users need access and refresh tokens to connect to the database in a session.
We need public and private keys for the database to sign and verify tokens for users using JWT (jsonwebtokens).
- Generate a pair of keys at cryptotools.net
The public key can be commited / visible, so we simply paste public key in template string within the 'config/default.ts' file:
export default {
...
publicKey: `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAUo7LMGfBTx51uw0KTUfAl35M
eEAVGuD5a+dNrM9meNg97rv/nC+tveDBIKRSKXBIHUQdwYczvgkHRPAJuYwCqibr
n/viZGA4s/ECUtYqLb4r9A6t49H0K0UVWNEEzDC7onsUXkjNle3kdY/rmoSCmuKx
OOrsHTZ2Oyb0pzUciQIDAQAB
-----END PUBLIC KEY-----`,
}
The private key must be hidden. Use dotenv file for this:
- in proj root
yarn add dotenv
- add
require('dotenv').config();
to top of 'config/default.ts' file - create dotenv file with bash
touch .env
- paste private key in '.env' as
PRIVATE_KEY
- add
privateKey: process.env.PRIVATE_KEY
require('dotenv').config();
export default {
...
publicKey: `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAUo7LMGfBTx51uw0KTUfAl35M
eEAVGuD5a+dNrM9meNg97rv/nC+tveDBIKRSKXBIHUQdwYczvgkHRPAJuYwCqibr
n/viZGA4s/ECUtYqLb4r9A6t49H0K0UVWNEEzDC7onsUXkjNle3kdY/rmoSCmuKx
OOrsHTZ2Oyb0pzUciQIDAQAB
-----END PUBLIC KEY-----`,
privateKey: process.env.PRIVATE_KEY
}