If you’re only looking for Node-Express-ES6 Boilerplate, click HERE
Let’s Get Started
- Download and Install Node.js
- Create a package.json file
- Install Express
- Folder Structure
Setting up Package.json
Setting up Typescript
You’ll need some global tools too. You might need to run these as sudo or “Run As Administrator” if you’re using windows.
npm install –global typescript gulp tsd
- typescript is our global typescript compiler
- gulp is a build tool that’s crazy popular now and will help us create beautiful expressive build commands
- tsd is a package manager for downloading TypeScript definition files. We’ll primarily use this for expressjs
Setting up tsconfig.json
Create tsconfig.json file at root level. This tells our typescript compiler some information about how to compile our .ts extension files. You can read more about tsconfig.json files here.
Setting up your Linting using tslint
Create tsling.json file under root directory for defining linting rules.
Setting up Gulp file
Create gulpfile.js at your root level which will handle all gulp compilation and cleaning work.
Now let’s start moving towards actual ./Src folder and actual server setup code.
In our src directory. We’ll need to start a typescript definition file.
tsd init -y
This will create one file “tsd.json” and one folder “typings” in your src folder. Now we’ll install express’s typescript definition with:
tsd install express –s
tsd install body-parser –s
How to write code in Server.TS?
Let’s define our first class “HttpServer”
– it will initiate and define express app
– handle bootstrapping express app
– handle defining routes
– and basic express configurations
We will get to the route files later on but first let’s start server and finish our server.ts coding
Helper functions like “onError” and “onListening” are here –
Setting up Express Routes
We have two routes – Index and Users for example.
Create “routes” folder under “src” folder which will have our routes.
Create Index.ts and Users.ts files inside routes folder.
Now final compilation and Build
> npm run gulp or gulp build
> npm run start
Hope this will help someone. Happy Coding