execute npm-task on every node-dev restart - node.js

I currently have a dev-setup with node-dev and iIrecently integrated tsoa into the setup to generate a swagger file and the express-routes for my app.
I already added all the necessary steps to generate my routes but right now its a manual process inside the dev-setup because I cant execute the needed npm-task on every restart of the application through node-dev.
So my question is, if its possible to start this task on every restart initiated by node-dev?
I hope someone can help me, and thanks in advance.

In your package.json add the following entry to scripts
"profit": "tsoa routes && node-dev -r tsc/register app.ts"
Then run it with
npm run profit
You can change profit to whatever you want

Related

Run nextjs application in the background

I have an application built with nextJs and this application should work on a local server (Windows).
my customer told me that he needed this application to work in the background after searching I found that I needed to use a package called pm2 and when I used it gives me an error and I found that I needed to make some configurations for it and I can't found any helping resources, please help 💔
I found that to run nextJs application in the background you will need a custom configuration
you need to download the pm2 globally in your system
create a file with the name ecosystem.config.js in the root folder next to the package.json file
you need to put your config data in this file which would be something like this
module.exports = {
apps: [
{
name: "inventory_test",
script: "node_modules/next/dist/bin/next",
args: "start -p 3333", //running on port 3000
watch: false,
},
],
};
you should set the name as the name you want to see when you check
the list of pm2
the problem will be solved when you set the script as I did in the code above to be more precise the default run of pm2 is to go to the node js folder in the system and try to make start for the application using npm directly but this is the problem we need to make it use the node runner from the nextjs itself or something like this so we change the script as above
after that, we set the arguments that we should run after the npm and in my example is the arg start and choose the port for our application too
and now we make our config
NOTES
you should make build before you start the application
to run the project you will open the folder of the project in the terminal || cmd || cmder and run the command pm2 start ecosystem.config.js

Refresh application when code changes on nestjs

What I want is simply refresh/reload the application when some change is made on the code. For now, I have to stop the server (ctrl + c on the terminal) and then run it again so the changes have any effect
Nest has a watch mode for this. Simply run npm run start:dev and nest will react to any change

Using BrowserSync with only Node.js (MEAN)

Can anyone help me understand how to use BrowserSync with an already running node server?
I know that I have to proxy the server localhost:3000 but where and how would I do that?
Since I'm not using Gulp/Grunt my best guess is proxying a running BrowserSync with express.js but what would that look like?
EDIT:
#RunnyYolk's answer is correct, The best way is to create a script in the package.json file that has BrowserSync proxy your node server. However the issue is more specifically how to use BrowserSync when you need to wait for a process to finish first, such as Angular compilation, then reload your browser.
My build process looks like this:
"build": "del-cli public/js/app && webpack --config webpack.config.dev.js --progress --profile --watch",
How do I call browser-sync reload after that webpack compilation?
I've had browser sync running with a few Node projects. I remember it being a little bit of a fiddle the first couple of times, but definitely worth the effort.
I can't remember exactly what I got caught on, but try these things and let me know if it's still not playing:
Assuming browser-sync is installed, make sure your app.js file (or whatever you've called it) has the server listening on port 3000.
Start the app with a command similar to this:
browser-sync start --proxy localhost:3000 --files="public/stylesheets/*.css", "public/scripts/*.js" "public/views/*.ejs" --no-notify
That works for me. "--files" is a list of files for browser sync to watch, and --no-notify gets rid of some notifications that get fired on each file change - possibly useful in some situations, but I found them annoying.
Looking at this command I notice that there is no reference to node app.js to start up the app. I assume (although I'm not certain) that bowser-sync looks inside package.json for the "start" script. So if my assumption is correct, and if the above command isn't working for you, check that your package.json file has the script included.

Running forever script from Google Cloud Platform App Engine startup script

I have edited the startup-script variable for one of my instances running on the Google Cloud Platform App Engine. I'd like it to call a forever script to make sure my node app is running. So I added:
cd /opt/bitnami/apps/myapp
forever start --workingDir /opt/bitnami/apps/myapp/ --sourceDir /opt
/bitnami/apps/myapp/ app.js
after the #!/bin/bash line (also tried without the cd as it's not really necessary based on my command). But once the vm is started, running a forever list doesn't list my forever task as having ever started. If I copy and paste that forever command into a gcloud terminal and run, the task shows up fine and my app starts no problem.
Am I not calling this correctly somehow within the bash script?
The simple answer is that GAE does this by default. No need for forever or PM2. There are certain health checks that GAE does on the Docker container holding your app, and if they do not pass the instance is automatically restarted
If you want granular control over these checks (called Legacy Health Checks) you can add this to your app.yaml file:
health_check:
enable_health_check: True
check_interval_sec: 5
timeout_sec: 4
unhealthy_threshold: 2
healthy_threshold: 2
There is also updated mechanisms (called Updated Health Checks) that are still in beta, but can be used instead
The proper way to start your nodejs app on appengine is to specify the "scripts" field in your package.json, as the documentation
Below is an example borrowed from this sample
"scripts": {
"start": "node ./bin/www",
"test": "cd ..; npm run t -- appengine/analytics/test/*.test.js"
},
If you however, are only interested in running a node script, and not interested in the features that come with Google app engine, then you may simply run it on a Google Compute Engine instance.

Running meteor.js application on your own server

I am trying to run my meteor application on my local network so that when I am off-line I can still use the application on any devices connected to my network. I have been following http://ox86.tumblr.com/post/45184656062/running-your-meteor-js-application-on-your-own-server as a basic tutorial on how to do this. I am new to mongodb and node.js, which causes some trouble. However it seems that everything is working except I do not know where my app is running or what the address is? For example after I complete step 4 from the tutorial
export PORT=8080
export MONGO_URL=mongodb://bill:123456#localhost:27017/dbName
after navigating inside my bundle where main.js is I use
forever start main.js
There are no errors, but I don't know where my app is or if it is even working. Once again I am new to this idea of hosting my own meteor app. Any ideas or suggestions of what I am doing wrong. Also an explanation of the process would be much appreciated. Thanks for the help!
It's likely you have errors. You should check with forever logs main.js to see what you get.
From what it looks like you may have missed the ROOT_URL variable too.
export ROOT_URL=http://www.yoururl.com
export PORT=8080
export MONGO_URL=mongodb://bill:123456#localhost:27017/dbName
forever start main.js
The best check would be to see what you get in the forever logs. Also you may have to install fibers (before you start your app) if you have some error in your logs like invalid ELF header
cd bundle/programs/server/node_modules
rm -r fibers
npm install fibers#1.0.1

Resources