To start the project
- Inside a development environment(watch mode)
$ npm run dev
- Build the project and run in production
$ npm run build
$ npm start
Remove or change the License, Code of conduct and Contributing.md (and maybe readme too) before you start using this template
📦project
┣ 📂src
┣ 📜.dockerignore
┣ 📜.eslintrc.json
┣ 📜.gitignore
┣ 📜Dockerfile
┣ 📜package-lock.json
┣ 📜package.json
┗ 📜tsconfig.json
package.json
&package-lock.json
contains the information about the npm metadata of the project like depencies, version, author, runnable scripts etc.tsconfig.json
contains the metadata about the typescript configuration for the project..eslintrc.json
contains the data about linting rules while using eslint..gitignore
describes which files to ignore when pushing the files to git.Dockerfile
can be used to get a production ready container image of the project..dockerignore
tells which files to ignore while building the docker image.- The
src
directory contains the source code of the project.
📂src
┣ 📂controllers
┣ 📂entities
┣ 📂enums
┣ 📂errors
┣ 📂middlewares
┣ 📂routes
┣ 📜app.ts
┣ 📜ormconfig.ts
┗ 📜index.ts
app.ts
contains the script to create the server. It exports an object namedapp
that connects to all the routes and middlewares.ormconfig.ts
is the configuration for typeorm. It exports a Datasource object that can be used to gnerate repositories for different Entities.index.ts
contains data about connecting the server to a port, environment variables, etc.
📂enums
┣ 📜HttpHeaders.ts
┣ 📜HttpStatus.ts
┗ 📜index.ts
HttpHeaders
contains some useful headers as enum. You may add more as per your project needs.HttpStatus
contains some statuscodes returned by the server on certain responses.- All the enums should be exported using the
index.ts
file. E.g. for a new file createdMyEnum.ts
, in the index.ts a new line should be added i.e.export * from MyEnum.ts
📂errors
┣ 📜BadRequestError.ts
┣ 📜CustomError.ts
┣ 📜DatabaseConnectionError.ts
┣ 📜ForbiddenError.ts
┣ 📜InternalServerError.ts
┣ 📜NotFoundError.ts
┣ 📜RequestValidationError.ts
┣ 📜ServiceUnavailableError.ts
┣ 📜UnauthorizedError.ts
┗ 📜index.ts
CustomError.ts
exports an abstract class that extends theError
class. All the other files(except index.ts) exports an error class. All the newly created error should extend theCustomError
class- All the errors should be exported using the
index.ts
file. E.g. for a new file createdMyError.ts
, in the index.ts a new line should be added i.e.export * from MyError.ts
📂middleware
┣ 📜ErrorHandler.ts
┣ 📜RequestValidator.ts
┗ 📜index.ts
Middlewares can be used to refactor the repeatative code at the starting of some routes. Especially useful for request validation, authorization etc.
ErrorHandler.ts
is used in theapp.ts
file inside thesrc
folder. It basically a global error handler and helps to send response in a certain format.RequestValidator.ts
is used to run validations on the requests.- Similar to error and enums directory, here also all the middlewares should be exported from the index.ts file also.
📂entities
┗ 📜index.ts
- Entities are database models. One object defines one row in that entity(table).
- While creating new entity, make sure that is exported from
index.ts
and also consumed in theormconfig.ts
in the constructor of the datasource.
export const AppDataSource = new DataSource({
...
entities: [UserEntity]
...
})
To share some insights we have created the UserEntity class
📂routes
┗ 📜index.ts
- Routes are the Rest endpoints definitions along which uses certain controller(s) and middlewares with it according to specify the business logic and validations.
📂controllers
┗ 📜index.ts
- Controllers are plain business logics in the project. Routes consumes these controllers to map them to certain endpoints.
To give an example how these routes and controllers are structured we have created Authentication routes as well. If you don't need to ue them, please delete those controllers and routes.