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.
https://github.com/bapatel1/nodejs-express-es6-boilerplate
- TypeScript
- express
- Node.js 6.x
- Gulp
- 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
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.
Create tsling.json file under root directory for defining linting rules.
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.
cd src
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:
cd src
tsd install express –s
tsd install body-parser –s
Create server.ts file under ./src for server setup.
– 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 –
- 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.
> npm run gulp or gulp build
> npm run start
Bhavin Patel
https://github.com/bapatel1/nodejs-express-typescript-boilerplate
https://itsmebhavin.wordpress.com/