In this post I'm going to create a very simple Web API in Node, that will run a Powershell command on the back-end to retrieve information about a running process. I'll be using three modules to assist me with this, express.js, nodemon and node-powershell.
Lets get started,
Create a new folder and open it with VSCode
Open the Terminal with Ctrl + `
Run npm init and except all the defaults
npm install express node-powershell nodemon
Edit the package.json file and the 2 lines below in the scripts section. a. "dev": "nodemon server.js", b. "start":"node server.js",
- In the root of the project create a new file and call it server.js and add the below code.
Execute npm run dev, open your browser and navigate to http://localhost:5000, you should get the results below.
- Next we need to import node-powershell, create a new shell instance and run a powershell command when we get a request.
In the example above we are running the Get-Process cmdlet and filtering on processes that contain the word "code".
Refreshing the browser will output the below results.
You will need powershell preintalled on you machine where node will be running. ( This is usually preinstalled automatically )
Thanks for reading.