You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Sample boilerplate project for node.js, express using TypeScript and Gulp.
Why TypeScript?
I believe that TypeScript has been embraced as the choice language for building next generation web application using ECMAScript 6 (ES6) with strong typing. Strong typing doesn’t necessarily improve the JavaScript that your Node.js server will execute, or the JavaScript that your browser might execute. However, it provides the developer more insight into public and 3rd party APIs as well as reducing the bugs (and development cycle to check for bugs) in the software we are developing.
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 compilergulp is a build tool that’s crazy popular now and will help us create beautiful expressive build commandstsd 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.
Server Setup
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.
cd srctsd 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:
cd srctsd install express –stsd install body-parser –s
Main server.ts file setup
Create server.ts file under ./src for server setup.
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.