Test task: an example of "production ready" API. Breakpoint 4 hours from the start of the task.
Dear {UserName}, do not upload the source code to the production server without revision.
npm start run:dev
To start, you need to have globally installed Nodemon.
npm start run:prod
Before start, you need to add the new typescript dependency in PM2.
(pm2 install typescript
)
Send request to endpoint http://localhost:4000/api/v1/auth with header:
Authorization: Bearer secret-api-key
In response you get a token:
{
"status": "ok",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEsImlhdCI6MTU5ODgyNTIyMn0.eyTEbIGN04OLh2KDIWGS_6eYyBlWkTwSZ6eD5-yC4Tc"
}
The token has no expiration date.
Add the token to the header for all the following requests.
Headers:
token: eyJhbGciOiJIUzI1N...
GET: http://localhost:4000/api/v1/users
or
DELETE: http://localhost:4000/api/v1/users/1
There are three locations for middlewares, each location has its own area of responsibility.
- The coverage area is the whole application.
src/index.ts
middlewares: [bodyParser.json, bodyParser.urlencoded];
- The coverage area is all routes in the controller.
src/controllers/usersController/index.ts
middlewares() {
return [jwtGuardMiddleware];
}
- The middlewares will apply only to one route
src/controllers/usersController/index.ts
routes(): IRoute[] {
return [
{
path: "/users/:id",
method: "delete",
action: this.deleteUser,
middlewares:[rateLimitter]
},
];
}
To set up a request validator, you need to create a file with a description of the incoming data, then connect it to the route.
Working example:
src/controllers/usersController/index.ts
import { createUserSchema } from "../../schemas/userSchema";
routes(): IRoute[] {
return [
{
// ...
validationSchema: createUserSchema,
},
];
}
src/schemas/userSchema.ts
export const createUserSchema = {
type: "object",
properties: {
name: {
type: "string",
minLength: 3,
},
},
required: ["name"],
};