Coder Social home page Coder Social logo

tridungle / express-typescript Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kkrishnakv/express-ts-starter

0.0 1.0 0.0 148 KB

Express JS Starter pack using Typescript and configuration based JSON for different environment.

License: MIT License

TypeScript 96.59% JavaScript 3.41%

express-typescript's Introduction

express-typescript

Express is a light-weight web application framework to help organize your web application into an MVC architecture on the server side. TypeScript enables us to develop a solid web applications using Express on Node.js.

  1. npm start
  2. It is used to run the project in development, it will reload the project while the source get changed.

  3. npm run build:prod
  4. It is used for the production build, which uses the webpack-cli for the build and provides a single output file with required additional file such as package.json, configuration files.
    The output file name and the files to copy can be managed in webpack.config.js

To run the project
  • Clone or download
  • Yarn install/npm install
  • npm start
  • http://localhost:40401/

Confiuration for different environment

 
we can add the json files for different environment /config/config.dev.json. 
  {
  "Config": {
    "port": 40401,
    "appConfig": {
      "version": "1.0.0",
      "name": "express-api",
      "env": "development"
    },...
    
 Configure the port db and etc...

IConfig.ts is an interface for the root tags to get the properties in the code. 
  export interface IConfig {
    appConfig: any;
    DBConnections: any;
    port: number;
}

To set the enviroment.
    export class AppSetting {

        public static Env = Environment.Dev;

        public static getConfig(): IConfig {
            let configManager = new ConfigManager();
            return cloneDeep(configManager.Config);
        }
    }

ConfigurationManager is class used to read the configuration json files using nconf. It will stored in memory for the first request and served from memory from the subsequent request.
     nconf.use('memory');
            if (!nconf.get('Config')) {
                this.getFile(filename);
            }
            this.Config = nconf.get('Config');
            if (!this.Config) {
                Logger.error('Unable to read the config file');

                process.exit();
            }

Configure the environment.
    export enum Environment {
        Dev, Production, Local
    }

  

Database Connectivity:
Sequelize is used to connect the database. The configuration is managed in config.{env}.json. The dialect options is used to configured the database server.

"DBConnections": {
      "default": {
        "user": "sa",
        "password": "sql@123",
        "options": {
          "host": "localhost", // server name
          "database": "testdb", // database name
          "requestTimeout": 300000,
          "dialect": "mssql",
          "operatorsAliases": false,
          "logging": true, //Enable the sequelize logger for queries for dev mode.
          "dialectOptions": {
            "encrypt": false
          }
        }
      }
    }

How to pass the Parameter to SQL Query

If the JSON and parameter name are same, we can pass the json object to the sql manager as below:
public addCustomer(customer: ICustomer) { let query = "INSERT INTO customers (name,address) VALUES(:Name,:Address)"; return this.db.Insert(query, customer); }

Adding parameter without JSON Object

public deleteCustomer(id) { let query = "DELETE FROM customers WHERE Id=:id"; this.db.addInputParameter("id", id); return this.db.Delete(query); }

http://localhost:40401/customers

Integrated Swagger UI. Add the swagger json in swagger-docs. Ref customer.swagger.json

Swagger.controller.ts for adding the swagger routes. export class SwaggerController {

public static configure(app: Express) {
    app.use('/swagger/customer', swaggerUi.serve, swaggerUi.setup(customer)); //route for the swagger ui
}

}

To run the swagger example http://localhost:40401/swagger/customer Authentication module using OKTA.

  • The API URL Can be excluded in the configuration file.
  • Decode and Retrieve the Username & Email  from the token.
  • Validate the JWT Token against the Client ID.
  • Display the Base API Path information without authentication.

Update the values in the configuration

"appSettings": { "excludedUrl": [ "info" ] }, "oktaConfig": { "url": "https://dev-142636.oktapreview.com/oauth2/", "clientId": "" },

excludedUrl - used to view the API without authentication.
url - OKTA Issuer URL.
clientId- The client id used in the Angular App.

https://www.initpals.com/node-js/express-js-application-seed-project-with-typescript/
https://www.initpals.com/node-js/how-to-use-json-based-configuration-in-express-js/

express-typescript's People

Contributors

kkrishnakv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.