Git Template is a frame for us to make numerous clones with the same configuration
Then we install Typescript by
yarn. I suggest installing it with
#For npm npm install --save-dev typescript #For yarn yarn add --dev typescript
We come to the main guy, Express server
- Install Express module. As I mentioned in Jest, Express can't understand TS, so we need an additional module,ts-node, to run the server on local, and 2 other modules @types/express, @types/node to get types of Express.
npm install express npm install --save-dev @types/express @types/node ts-node
There are some other ones you may need nodemon for watching the changes in the resource folder, dotenv for loading environment variables files, or cors for solving error "access-control-allow-origin".
Create our server. From my experience, we need to create 2 files in
srcfolder placed at root level. The first one is
src/config/express.tswhich is used to config our express server, and the second one is
src/index.tsfor starting the server. If we merge 2 of those files, we will violate the SOLID theory.
Add scripts to
package.jsonto start server. Thanks to
ts-nodewe can directly start server without continuous complier.
"start": "ts-node -r tsconfig-paths/register src/index"
Add Unit tests to make sure everything works as expected. In case you followed my configuration in this post, then push test files into folder
__tests__placed at root level with the same location in
src(your folder tree will look like below). I love using supertest to test my express server, you can make this page as your reference
Finally, we come to the last part. After access our repository on github, we tick the box template repository in tab setting
CONGRATULATION!!! EXPRESS TYPESCRIPT BOILERPLATE ACHIEVED
You can try to use it by clicking on Repository template on New repository page
This is my Template, I am glad if you give me a star 😍.
We have gone on a long journey with the Create Your Own TypeScript Express Template series. Thank you very much. If You need GitHub template, you can refer here
I am really happy to receive your feedback on this article. Thanks for your precious time reading this.