Express is HTTP framework for NodeJS and I think to practice using it by writing an application to find and replace a given string by another string. At the time of writing this sample, Express v5 is still under development, so we install it as follows:
npm install express@nextAs always our project has configuration, so we need to have dotenv:
npm install dotenvAfter that, we are going to have Typescript:
npm i -D typescript @types/express @types/node
npx tsc --initFor parsing request data based on content-type header we need the body-parser library:
npm install body-parser
npm i -D @types/body-parserFirst you need to create .env as follows:
PORT=8080
TARGET="Foo"
REPLACED="Bar"Then build:
npm run buildAnd, run the code:
npm startWe are ready to test it:
curl 127.0.0.1:8080/api/replace -H 'Content-Type: application/json' -d '{ "Hello": "Foo", "Bye": { "Name": "Foo", "Who": 1 } }'Running all above commands each time you change something takes time. So you can use the dev command
as follows:
npm run devWe have an API named replaced that replaces a given string with another string in the request body.
Request body is JSON structured, and we convert it into dictionary, so we can do the replacing process
on values and don't change the keys.
