TeamCity, NodeJS and API testing - node.js

I'm setting up CI/CD which includes TeamCity as build server.
I have set up 3 build steps:
npm install,
node server.js,
node run_tests.js
My server.js file runs a REST API via EXPRESS --> which I realized blocks TeamCity in running step #3...which I now understand why...(because everything is run by the same "window" when TeamCity executes it - because it doesn't not exit again...but keeps listening as expected of an API).
How do I run the API, call it and test/confirm it works, shut it down again and then continue with the next step...?

Solved!
Start the app/API via PM2
Execute tests which uses the API
Stop the app/API via PM2
:-)

Related

Error running nextjs with PM2 on Azure App Service on Linux

The pipelines and releases seem to work finely as they get the artifact deployed on the app service or so it seems as the output error happens on start of the PM2 process.
I haven't found anything on this error on google, and don't know if there is a way to update PM2 on the app service machine as PM2 itself suggests.
These are the logs.
Error message
summary image
I was expecting the app to work and no longer show an application error as I fixed both build and release pipelines.
This is a picture of the important bit of info of the error as its really long
Check the below steps to run nextjs with PM2.
First create a nextJS app.
Run npm run dev command to run the app in development.
Deploy the app to Azure Linux App Service.
Open the SSH of the Azure App Service to run the PM2 commands.
We can open directly with the below URL.
Path to SSH - https://YourDeployedAppName.scm.azurewebsites.net/
OR
Navigate to the deployed Azure Linux App => Advanced Tools => Go => SSH.
Run the below command to install PM2.
npm install pm2 -g
Thanks #Azure OSS Developer Support for the commands.
In Configuration section => General Settings add the Startup command.
pm2 start site/root/index.js --name mynpmnextapp
Your path may differ for the index.js file.
My app entry point is index.js.For some apps it can be server.js.Change it according to your app.
cannot find module `../build/output/log'
Make sure you are not running the PM2 with the output folder. As mentioned above it has to be the entry point either server.js or index.js based on your code files.
don't know if there is a way to update PM2 on the app service
In the KUDU Console use npm install pm2#latest -g to update the PM2.
References taken from MSDoc and npmjs.

How can I add a response header when running a SPA in Azure Web Apps using PM2

I have a React SPA which runs in Azure Web Apps, Node stack (Node 16 LTS), via the startup command:
pm2 serve /home/site/wwwroot --no-daemon --spa
I would like to add a response header (specifically, a Content-Security-Policy header) for every outgoing request.
Things I have tried:
adding a .htaccess file
adding a web.config file
looking for an evironment setting, or way to configure either pm2, or node
I don't have any node code to change (so I can't add headers there), and doing it in React feels "too late" - I think it's something that node on the server, or Azure needs to do.
My nuclear option is to wrap Front Door around it and do it there, but I'm hoping there is way to achieve this without that.
In App Service Linux, React application can be started with PM2, npm start or custom command.
The container automatically starts your app with PM2 when one of the common Node.js files is found in your project:
Also Note that Starting from Node 14 LTS, the container doesn't automatically start your app with PM2. To start your app with PM2, set the startup command to pm2 start <.js-file-or-PM2-file> --no-daemon.
Be sure to use the --no-daemon argument because PM2 needs to run in the foreground for the container to work properly.
Reference: https://learn.microsoft.com/en-us/azure/app-service/configure-language-nodejs?pivots=platform-linux

Azure Pipeline Node.js run localhost server(express) and client at different port to run jest tests

I am running a client application(JS) using Node.js on port 8080. At same time, I am running an Express Server using Node.js at port 3030.
Integration tests are written in Jest and Selenium. To test app, on my local machine, I open three Powershell windows and run Integration tests. It works as expected and test case passes.
Server Folder (new Terminal)
npm install
npm run start (Express.js)
Client Folder (new Terminal)
npm install
npm run start
Client Folder (new Terminal)
npm run tests
In Azure Pipeline, I created YML for image 'window-latest', However it stuck at Server Start.
powershell:
display name: Server Start
npm install
npm run start
powershell:
display name: Client Start
npm install
npm run start | npm run test (Combined in package.json)
If I remove Server Start in Azure Pipeline, test cases that do not need server pass.
Both projects are under one repository. I use cd command in Package.json to install and start server at server location.
start:server: "cd /server/dev/ && npm install && npm run start"
start:client: "npm install && npm run start"
test: "npm run start:server | npm run start:client | jest"
npm run test, works fine on my local machine, server & client runs and test cases pass(single terminal). On Azure Pipeline, I need to Cancel task manually because the Azure pipeline task never ends. After separating scripts(Powershell), I noticed pipeline stuck on Express.js Server Start(Tested by running single and combined npm commands in YAML).
Question: How can I start localhost Express Node.js server in Azure Pipeline before starting Client?
You may check start-server-and-test:
Starts server, waits for URL, then runs test command; when the tests
end, shuts down server.
Microsoft does not allow binding to a port on Hosted Agents.
You must own a self hosted agent to do that.

Heroku: Disable default behaviour of - Npm run start

Every time I deploy code to Heroku, it automatically looks to run the start script in package.json (NodeJS)
My app is set to run on a timer / scheduler and as such does not need to be run by Heroku when deployed.
How do I disable this behaviour?
you should create a One-Off Dynos whith a Heroku Scheduler add-on.

Node.js deployment to production in windows server

I'm fairly new with node.js platform.
I'm working on deploying a node.js app to production in windows sever.
Currently I'm using forever for my production server, and pm2 for development server(just to see how different process managers work.)
What I noticed is my app is down after a while (normally couple days).
I think it might be because my user session which runs the process is closed.
Can you share with me the "correct" deployment steps?
The steps I take to deploy are:
1. Run node cmd as administrator
2. Go to site root
3. Run forever start server.js or pm2 start server.js
Thanks in advance.

Resources