Simple REST API.
Express
Typescript
Mongodb
Mongoose
node
installedhttp client
git clone https://github.com/wojciechszmelczerczyk/ts-express-products-api.git
cd /ts-express-products-api
npm i --force
Create .env
file in project root directory.
# Some port number
PORT=
# MongoDB uri
DB_URI=
npm run dev
You can import provided postman collection to test api.
Library used to develop app architecture routing-controllers. REST API using Controller
, Service
and Repository
approach.
GET /api/products
User send request to the server, server query database and find all products.
GET /api/products/:id
User send request to the server with provided id parameter.
Middleware validate if provided id has correct syntax.
If syntax is incorrect API returns 400
with error message.
If provided id has correct syntax, middleware pass handler to the server.
Server try to query product with specific id from database.
If product doesn't exist, API respond with 400
and error message.
POST /api/products
User send request to the server with name
and price
data.
If no data provided or data is invalid, error middleware intercept error, modify error message and return to the client with 400
status.
Otherwise new product is being returned.
PUT /api/products/:id
User send request to the server with id
parameter and name
, price
data.
Middleware validate if provided id has correct syntax.
If syntax is incorrect API returns 400
with error message.
If no data provided or data is invalid, error middleware intercept error, modify error message and return to the client with 400
status.
Otherwise updated product is being returned.
DELETE /api/products/:id
User send request to the server with id
parameter.
Middleware validate if provided id has correct syntax.
If syntax is incorrect API returns 400
with error message.
Otherwise handler is being passed to controller where server validate if product with provided id exists.
If not 400
with error message is returned.
When deletion of product is successful, server respond with 204
status.
Method | Endpoint |
---|---|
GET | /api/products |
GET | /api/products/:id |
POST | /api/products |
PUT | /api/products/:id |
DELETE | /api/products/:id |