Showing posts with label Express. Show all posts
Showing posts with label Express. Show all posts

Monday, December 6, 2021

How to restart the node server after the change in the typescript files in Node Express Server development?

 usually we can use the following configuration in the Package.json file to run the node server with nodemon.

"start:dev": "nodemon --watch './**/*.ts' --exec ts-node src/index.ts"

how the above command will required additional configuration for the nodemon.

ts-node-dev will provide us much better experience since there is not setup configuration. we can simply execute the command below to start the node express server and any change is made the server will automatically recompile and restart.

 "dev": "ts-node-dev src/index.ts"

However you must install the ts-node-dev package to run the command

yarn add ts-node-dev

Sunday, July 18, 2021

bodyParser had been deprecated and re-added to express.json.

 Usually we use bodyParser from body-parser library to parse the json object in the express server.

after we upgrade the express library to 4.16. we should receive the following message.

The signature '(options?: OptionsJson & OptionsText & OptionsUrlencoded): NextHandleFunction' of 'bodyParser' is deprecated.ts(6387)

index.d.ts(19, 5): The declaration was marked as deprecated here.

before we use this one to limit the JSON object size.

app.use(bodyParser.urlencoded({limit:'25mb', extended:true}));
app.use(bodyParser.json({limit:'25mb'}));

currently it directly interact with express object

app.use(express.urlencoded({limit:'25mb', extended:true}));
app.use(express.json({limit:'25mb'}));


Monday, June 29, 2020

how to use environment variable in Node.js and Express Server development?

it is very convenient to use environment variable during the web development with express server and Node.js

here is the steps to configure the environment variable in the  project.

1. add a .env file to the project folder.

2. install the dotenv package
    npm i dotenv -S

3. add dotnet library to the file that will load the environment variable. in my case it is from a controller

dotenv = require('dotenv');
dotenv.config();

4. add required variables to the .env file

#test token
TOKEN=recipeT0k3n

# API
API_TOKEN=recipeAPIT0k3n

5. load the environment variable, it must be the same file in the step 3

const token=process.env.TOKEN || "recipeT0k3n",

or you can define it as global variable in the index.js or main.js with the following snippet of code

dotenv = require('dotenv');
dotenv.config();
export const token=process.env.TOKEN || "recipeT0k3n",