A starterkit for building Node.js / Express.js application using Typescript. Currently I use it for setting up and running a RESTful backend in Node.
$ git clone https://github.com/abrarShariar/node-typescript-starterkit.git
$ npm install
$ npm start ./app
/classes
/controllers
/database
/routing
server.ts
Application controllers are placed in the controllers directory. Controlles are typescript classes with methods to handle http request and response from the router.
Example:
import{Router,Request,Response}from'express';import{PersonModel}from'../database/models/person.model';import{Person}from'../classes/Person';exportclassHomeController{staticpersonModel=newPersonModel();constructor(){}staticgetHome(req:Request,res:Response):Response{returnres.send("Welcome To API Home !!");}staticgetAllPerson(req:Request,res:Response):Response{letdata=HomeController.personModel.getAllPerson();returnres.send(data);}}Database settings and models are placed in the database directory. It contains a db.config.ts file and a models sub-directory for Model classes. Here I used MongoDB with mongoose. Other databases can be used by making some changes to the config file and model classes:
import{connect}from'mongoose';/** Database connection to mongoDB*/exportclassDBConfig{staticconnectMongoDB(){connect('mongodb://localhost/testDB',(err)=>{if(err)console.log("Faied to connect to DB");console.log("Successfully connected to MongoDB");});}}Sample Model class: person.model.ts
/** model and schema for Person*/import{Schema,Model,model}from'mongoose';import{Person}from'../../classes/Person';exportclassPersonModel{privatepersonSchema: Schema;privatepersonModel: any;constructor(){this.personSchema=newSchema({id: Number,name: String,email: String,skills: [String]});this.personModel=model('person',this.personSchema);this.personModel.find({},(err,data)=>{if(err){console.log(err);}else{console.log(data);}})}createPerson(person: Person){returnnewthis.personModel(person);}getAllPerson(){this.personModel.find({},(err,data)=>{if(err){return"ERROR in fetching People data from DB";}else{returndata;}});}}The routing directory contains the api.routes.ts file where all the API endpoints for the application is written.
All routes defined here starts with the /api/. For example: localhost:8080/api/home/
import{Router,Request,Response}from'express';import{HomeController}from'../controllers/home.controller';constrouter: Router=Router();/** API endpoints are defined here * all routes start with /api*/router.get('/',(req: Request,res: Response)=>{res.send("Welcome to API routes");});router.get('/home',(req: Request,res: Response)=>{HomeController.getHome(req,res);});router.get('/home/person/all',(req:Request,res:Response)=>{HomeController.getAllPerson(req,res);});exportconstApiRoute: Router=router;server.ts is the entry point of the application.
import*asexpressfrom'express';import{ApiRoute}from'./routing/api.routes';import{DBConfig}from'./database/db.config';import{connect}from'mongoose';constapp: express.Application=express();constport: number=process.env.PORT||8080;app.use('/api',ApiRoute);app.listen(port,()=>{DBConfig.connectMongoDB();console.log(`Listening at port :${port}`);});