How to add namespace to SOAP request using soap-node – Node.JS
Node.js , Requests / February 24, 2019

Q  I have XML request in the following format. How to add the XML NAMESPACE to the request JSON I’m going to send to SOAP server?

A: There are three namespaces defined in the XML. When adding them to JSON you can add them to the attributes key in the following way

   

logging in Node.JS – Express Application using Winston logger
Express.js , Javascript , Node.js , Typescript / February 20, 2019

Logging is keeping a record of all data input, errors, data output, and final results in a program. While developing an application, keeping track of responses, requests, errors, etc makes the development process more efficient. Keeping logs is also a part of code standardization. Logs can be displayed on the console and written to a file while working with Node.js.Here in this tutorial, we will learn how to create create a logger which keeps track of Request, Response, Errors and Time while building an ExpressJS application in Javascript/Typescript   Step 1 Install the Winston node package with the Node package manager

  Step 2 Create a logger.ts (.js file if you are working with  Javascript) preferably in a common folder outside, and add the following code to it  

In the above code, createLogger creates a new logger    

Adds Timestamp to your log. It defines formatting options  

Displays the log in console.  

Writes your error file to error.log and info to info.log    

If false, handled exceptions will not cause process.exit   Step 3 In your controller or where ever you want to create the log, import the logger file

Step 4…

Using Sequelize with typescript and express : Import all model files
Typescript , Uncategorized / February 17, 2019

STEP 1    Install Sequelize Install the latest version of Sequelize

Now create your migration script STEP 2  Initialize your project To create an empty project you  need to run init command

This will create seeders, config, migrations and models folders Modify the configuration file in your config folder accordingly   Step 3 Create your model and migration files

This will create Migration files in the migrations folder and model files in the models folder Step 4 Using models with typescript and express When you create multiple tables multiple model files will be created. To use all these model files at once, create a model folder in your project directory, create an index.ts file and include all the files using the fs module and path module of Node.js   The index.ts file will look like this  

The code

finds the model folder which is  outside your server files folder __dirname – Gives the current directory in which the executing file is located

Finds all .js model files inside the models folder

sequelizeconnection.import  import model files.In this case, it imports all model files

Above code adds the model to the model object

The…

Extend Express Request and Response : Typescript Declaration Merging
Typescript / February 12, 2019

When we use  TypeScript to Build a Node API with Express, you might need to add some additional properties to the Express request and response object. To add extra properties to the request and response object you need to extend the response and request interface. index.d.ts files are used to provide typescript type information about a module that’s written in JavaScript.For express, the index.d.ts is present inside the @types/express folder inside the node_modules folder.   As you can see in the index.d.ts file

The Request and Response extend core.Response and core.Request Extending Request and Response Object with Declaration Merging Declaration merging means that the compiler merges two separate declarations declared with the same name into a single definition. See below example in which the interface declarations are merged in typescript

So boy Object can have all the properties of both the interfaces. If the declaration was not merged adding property mark to the Object could have caused “property does not exist” error To achieve Declaration Merging with request and response interface follow these steps 1 Create a type.d.ts file in the root folder of the project and add the following code into it

2 Add the type.d.ts in the files of your tsconfig.json…

Environment variables and NodeJS Application – Linux
Uncategorized / February 11, 2019

The system environment is primarily the set of variables that define or control certain aspects of process execution. Environment variables help you differentiate between various environments in which your app is running and multiple apps in the environment can access these. Environment variables are system specific and are editable.   Environment variables help you to run your Node.js code in different environments like local/production/development systems. Various environments that we come across are local – Your local machine  environment sit – system integration testing environment uat – user acceptance testing environment development – Your development machine environment production environment staging environment   LINUX Method 1 To set environment variable use code

See your environment variables in Linux

This command displays environment variables Example output

  Now access environment variables inside Node.js code

Gives output as local the above code gives local as your environment Method 2 Accessing environment variables in Node.js is supported as a default. When your Node.js process starts it will automatically provide access to all existing environment variables.

(app.js is your entry file)

Gives output as local Method 3 using dotenv module

  Require this module using

Now create .env file in the application root…

Share this page in social media platforms