This is an attempt to build a simple REST API with Express, TypeScript and MongoDB, and unit testing with Jest. Feedback and contributions welcome!
You will require these programs installed on your machine:
- NodeJS and npm
- MongoDB
You can use Postman to test the API endpoints.
- Install required dependencies with
npm install
- Create a
db
folder in the project root and start MongoDB withmongod --dbpath=db
- Create a
.env
file based on the.env.example
. You can change the parameters if you want - Initialize the database data by running
npm run init-db
- Start the server with
npm start
You will have to provide a JSON Web Token to access the protected endpoints:
POST /auth
Body:
{
"username": "raul",
"password": "pwd"
}
Provide the following header in each of your requests:
Authorization: Bearer <your token>
You can create and view messages that will be stored in the database:
POST /message
Body:
{
"message": "test"
}
And to get the list of existing messages:
GET /message
npm test