Backend Developer (Node.js) test
TODO list
- API for user CRUD
- Logging strategy
- User auth
- to link to each other
- check nearby friends
- Implements by
- GET /user/nearby Doc
- Implements by
Project Structure
Request flow looks like:
Request
--->
Controllers
--->
Services (if need)
--->
Models (if need)
<---
<---
<---
Response
The main roles are Controller, Service, Model.
Corresponding to MVC design pattern, we can see:
- (C) Input logic: handling with Controllers
- (M) Business Logic: handling with Service (with Model if need)
- (V) UI Logic: handling with ViewService (it host the templates at
src/view
)
Testing approach
100% test coverage with e2e test (for auth testing):
Extensiblity
For good extensiblity, implement a simple dependency inject.
For example, if we get a Auth Controller, it will build instance tree like:
- AuthController
- AuthService
- UserModel
- MongoDB (Singleton)
- TokenModel
- MongoDB (Singleton)
- CacheService
- Redis (Singleton)
- ViewService
How to run
Before start
This project requires mongodb
and redis
. We should config them first, and the
config file located src/config/config.default
:
// src/config/config.default.ts
export const mongo: DataSourceOptions = {
type: 'mongodb',
host: 'localhost',
port: 27017,
username: 'root',
password: '',
database: 'test-backend'
};
export const redis: RedisOptions = {
port: 6379
};
// ...
We can config the options above to connect to exist DB, or using docker:
docker run -p 27017:27017 -it mongo
docker run -it -p 6379:6379 redis
If the MongoDB is good, we should create geographic index first:
$ mongo
> use test-backend # database name configured in src/config/config.default
> db.user.createIndex({ location: "2d" } )
Run test-backend
# install dependencies
npm install
# run app
npm run dev
Run test with coverage
npm run cov
Run thrid party app (for auth test)
npm run test-thrid-party-app
Reading Guide
For document reading, recommend order:
For code reading, recommend order:
- @Provide register class, and getInstanceByClass new class.
- @Middleware register middlewares, and loadMiddlewares.
- @Get register router, and loadControllers.
- service.test.ts for business logic .
For auth implement, recomend reading:
- Document: AuthFlow
- Code: Auth APIs, 3rd-party Test App and e2e test
For nearby following implement, recomend reading:
Last
Thanks for reading, I'm looking forward to our collaboration.