πΎ This is a simple express microservice. Client can get all quotes, one quote by id, delete and update quote by id. Also clinet can post a new quote and if required fields are not provided - random quote is fetched from a downstream API
- Implement an HTTP API web server microservice.
- Interact with downstream APIs.
- Connect server to NoSQL database .
- Test API endpoints and mock downstreams API requests.
- Node | Express
- MongoDB | Mongoose | MongoDB Atlas | MongoDB Compass
- Jest | Supertest | Nock
- Node Fetch
Local Server
'http://localhost:3000/api/quotes'
GET
POST
'http://localhost:3000/api/quotes/:id'
GET
PATCH
DELETE
Production
https://apiquotesapi.herokuapp.com/api/quotes
GET
POST
https://apiquotesapi.herokuapp.com/api/quotes/:id
GET
PATCH
DELETE
- GET
http://localhost:3000/api/quotes
[
{
"_id": "5fa1cab353459e9f3d03361b",
"quote": "If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place.",
"author": "Nora Roberts",
"createdAt": "2020-11-03T21:25:07.226Z",
"updatedAt": "2020-11-03T21:25:07.226Z",
"__v": 0
},
{
"_id": "5fa1cab553459e9f3d03361c",
"quote": "A goal is a dream with a deadline.",
"author": "Napoleon Hill",
"createdAt": "2020-11-03T21:25:09.678Z",
"updatedAt": "2020-11-03T21:25:09.678Z",
"__v": 0
}
]
- Utilized hooks to connect to local MongoDB before all tests run and disconnect after all tests finish running. Also seed testing database with testing data before each test and delete data after each test.
beforeAll(async () => {
beforeAll(async () => {
await mongoose.connect('mongodb://127.0.0.1/testing', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
})
beforeEach(async () => {
for (const testQuote of testQuotes) {
const newQuote = new Quote({
quote: testQuote.quote,
author: testQuote.author,
})
await newQuote.save()
}
})
afterEach(async () => {
await Quote.deleteMany()
})
afterAll(async () => {
await Quote.drop()
await mongoose.connection.close()
})
- Utilized Nock npm package and mocked downstream API response
test('POST | save quote to database with external api data', async (done) => {
const mockedResponse = [{ text: 'mock quote', author: 'mock author' }]
nock('https://type.fit').get('/api/quotes').reply(200, mockedResponse)
...
})
- All tests are successfully passing
PASS api/tests/quotes.test.js
Quote API endpoints
β GET | get all quotes from database (42 ms)
β POST | save quote to database with user data (28 ms)
β POST | save quote to database with external api data (20 ms)
β GET | get quote from database by id (11 ms)
β GET | returns message if quote id doesn't exist (10 ms)
β PATCH | update quote by id (44 ms)
β PATCH | returns message if quote id doesn't exist (12 ms)
β DELETE | delete quote by id (11 ms)
β DELETE | returns message if quote id doesn't exist (7 ms)
β GET | Default response if endpoint doesn't exist (7 ms)
β POST | Default response if endpoint doesn't exist (7 ms)
β PATCH | Default response if endpoint doesn't exist (10 ms)
β DELETE | Default response if endpoint doesn't exist (7 ms)
Test Suites: 1 passed, 1 total
Tests: 13 passed, 13 total
Snapshots: 0 total
Time: 3.807 s