Node.js Logging to File deleting old log on restart - javascript

I have a Node.js Application that I execute with systemctl start app.service on my server.
I configured my package.json correctly that on app start the log is being written in app.log with
node app.js > app.log 2>&1 the problem is now that with every restart the old log is being deleted and a new one is genereted. I want to keep my old log data for debugging purposes. How can I edit the log statement that the old log will be saved or the new log just appended? Is this possible?
I already searched in Stackoverflow and Google for a solution but did not find one. I was expecting to keep my old logs.

When using IO redirection, if you want to append to a file rather than overwrite the file, you need to use ">>" instead of ">". For example:
node app.js >> app.log

Related

Cannot GET an Express Route

I am working with ExpressJS route and everything works not until I added a route in which I get the error message,
Cannot GET /api/users/abc
using Postman.
Below are images that describe the challenge right now.
From my server.js file
To my userRoutes.js file
Every route works fine but /user_stats route doesn't. My approach to debugging the problem is to create an /abc route but to no avail, I got this HTTP response from Postman
Now the question, what might have gone wrong in my code?
It seems after adding the route you haven't started the server. So, to make sure every time you made any changes to the files get reflected in the server build, you should install nodemon and make it run.
npm install --save-dev nodemon // only for dev
To run :
nodemon [your node app]
For more details check this, how you can configure your server to run the build files.

Git return "'pull' is not a git command. See 'git --help'" from remote on Windows 10

I have a bonobo server on my server that is running windows 10.
I'm trying to link a new hook on "Update", so the files from the node development server can get updated directly from git.
I tried to do so using a cmd file, examples to follow:
update file in hooks
cmd //C "start C:\webServer\node\testServer\update.bat"
update.bat on the same folder on testServer
set PATH=%PATH%;C:\Program Files\Git\cmd\git.exe
cd C://webServer/node/testServer
git pull
That was returning the following error:
git: 'pull' is not a git command. See 'git --help'
I did some research and found out this post "git pull" broken where they talk about using -exec-path on Mac, I tried to search the equivalent in Windows and found out some variables for path related issues, tried them an none of them worked, also tried to reinstall git without any other result, at that point I was tired of it and I tried to use a node library, simple-git, that allows me to do a pull request using node specifying the path! I tried it on the server, and it worked! (I will attach the code of the file below) so I thought to try it from the remote server, but then I got the same error again, pull is not a git command, I tried to call the node file in different ways, I tried some python script to run a cmd command that runs the node server, I tried using bat and sh files, even using node to run another instance of node, and nothing worked, it always returns pull is not a git command :/
Node simple-git code
require('simple-git')("C:\\webServer\\node\\testServer")
.pull(function(a,b){
console.log(a,b); //for debug
})
Has additional information, git is properly set up as an environmental variable and I can access it without problems from the server, this issue only happens when trying to execute the pull from the hooks!
If anyone can give me some tips on what to try that would be awesome, thanks!

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Node.js Forever.js uptime stopped

I'm running a node script on my EC2 instance. I 'm using forever.js to run it because I'd like to run it even when I disconnect from my SSH connection.
I run:
forever start app.js
and then list running processes using:
forever list
and for uptime is says STOPPED no matter what I do.
ubuntu#ip-xx-xxx-xxx-xxx:~$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 79f1 /usr/local/bin/node app.js 17099 17100 /home/ubuntu/.forever/79f1.log STOPPED
You could just run forever logs yourfile.js to see logs. If you need to be sure that you are seen the right logs.
I checked the log file mentioned (duh!) /home/ubuntu/.forever/79f1.log and that had the error.
Turns out, it was a simple path error in my app.js script. I was using a file which wasn't in the same directory from which I ran forever start app.js. So it actually had nothing to do with forever.js, but was a simple path error where node couldn't find a file. Thank goodness for log files!

Heroku(Cedar) + Node + Express + Jade Client-side javascript files in subdirectory work locally with foreman+curl but not when pushed to Heroku

I am very new to node and heroku and I suspect this is some kind of simple permission issue etc, but I can't seem to track it down.
I have several pure javascript files in a sub-directory one level beneath my root directory where my web.js file is sitting. I have a line in my web.js file to specify the directory
app.use('/heatcanvas',express.static(__dirname+'/heatcanvas'));
If I run my app locally with Heroku Foreman I get the expected js response when I run the following curl command
curl localhost:5000/heatcanvas/heatcanvas.js
However when I push to Heroku and hit the corresponsing live url in the browser
www.example.com/heatcanvas/heatcanvas.js
I receive the following:
Cannot GET /heatcanvas/heatcanvas.js
If I check Firebug and/or the Heroku logs I see I am actually getting 404 errors for those files even though the pathing should match what is being done locally. It is also worth mentioning that third party javascript is coming over just fine, it is only when the src attribute of the script tag points to my site that there is an issue. What do I need to do to get my scripts to be available?
I recommend you to use process.cwd() value to get specific directory
process.env.PWD = process.cwd()
at the very beginning of your web.js
let you access files easily.
You can do
app.use('/heatcanvas',express.static(process.env.PWD+'/heatcanvas'));
instead of using
__dirname
Warning: Make sure to execute web.js at the root directory of web.js (Heroku web.js are executed that way)

Categories