Using nodejs app as Windows Service causes EPERM-4048 ERROR - node.js

I'm using "node-windows" for creating service that will run my app.js server
on windows.
This is the code that makes the service (using node-windows module)-
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Demo Landing Page',
description: 'The nodejs.org example web server.',
script: 'app.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
All works fine with the script but this line for reading directory content-
fs.readdir([PATH], function(err, builds)
This is the error that I get -
{Error: EPERM: operation not permitted, scandir [PATH]
errno: -4048,
code: 'EPERM',
syscall: 'scandir',
path: 'app.js' }
But If I run not as a service - just as-
node app.js
all works fine, permissions are fine.
What can I do in order to fix this?
Or maybe there is another option for running nodejs on windows server?
Thanks!
** I sign in as administrator and I have access to the following dir

Right click on service
open its properties
check what account its using under logOn
Either try this in service above code
svc.logOnAs.domain = 'mydomain.local';
svc.logOnAs.account = 'username';
svc.logOnAs.password = 'password';
or you can directly add credentials and restart the service

Related

Right way to connect to Google Cloud SQL from Node.JS

I followed the example on how to set up Node.JS to work with Cloud SQL, and generally got it to work, but with some workarounds on how to connect to the SQL server. I am unable to connect in the proper way passing the INSTANCE_CONNECTION_NAME to the socketPath option of the options variable for the createConnection() method. Instead, as a temporary workaround, I currently specify the server's IP address and put my VM IP address into the server's firewall settings to let it through.
This all works, but I'm now trying put it together properly before publishing to AppEngine.
How can I get it to work?
The following code works fine:
function getConnection ()
{
const options =
{
host: "111.11.11.11", //IP address of my Cloud SQL Server
user: 'root',
password: 'somePassword',
database: 'DatabaseName'
};
return mysql.createConnection(options);
}
But the following code, which I am combining from the Tutorial and from the Github page, which is referred to in the Tutorial, is giving errors:
function getConnection ()
{
const options =
{
user: 'root',
password: 'somePassword',
database: 'DatabaseName',
socketPath: '/cloudsql/project-name-123456:europe-west1:sql-instance-name'
};
return mysql.createConnection(options);
}
Here's the error that I'm getting:
{ [Error: connect ENOENT /cloudsql/project-name-123456:europe-west1:sql-instance-name]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'connect',
address: 'cloudsql/project-name-123456:europe-west1:sql-instance-name',
fatal: true }
What am I doing wrong? I am concerned that if I publish the app to AppEngine with the IP address, I won't be able to allow the incoming traffic into the SQL server?
I met similar error while testing 'coud sql'.
error message : Error: connect ENOENT /cloudsql/xxx-proj:us-central1:xxx-instance
solution :
+----------------------------------------------------------+ wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O
cloud_sql_proxy chmod +x cloud_sql_proxy sudo mkdir /cloudsql;
sudo chmod 777 /cloudsql ./cloud_sql_proxy -dir=/cloudsql &
=> now node js server can connect to mysql
refer to guide : https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-sql
Are you deploying your AppEngine app to the same region as the SQL database? (europe-west1)
The documentation at https://cloud.google.com/sql/docs/mysql/connect-app-engine states "Your application must be in the same region as your Cloud SQL instance."

Can't get os-service app working except from command line

I have a node.js app that I want to run as a windows service, and I'm using os-service to try to achieve that.
The service installs seemingly correctly, and the Windows Services management console provides this info about it:
In the listing:
Name: wibble
Description: (blank)
Status: (blank)
Startup Type: automatic
Log On As: Local System
In the properties/General pane:
Path to executable:
"C:\Program Files\nodejs\node.exe" "C:\path\to\app.js" "--run"
Attempting to start this service using powershell thus
Start-Service wibble
produces an error message containing: Cannot start service wibble on computer '.'
Attempting to start this service from the management interface's "Start the Service" link with the service selected in the Services manager yields a different error message: Windows could not start the wibble service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion.
The following works, when executed from a powershell in same folder as the app. Clients can connect to and use the app successfully:
node app.js --run
However, the powershell stops at that point until CTRL+C stops the service.
Here's the JS I added to the app to get it to run as a service:
var service = require('os-service');
var fs = require('fs');
process.chdir(__dirname);
if (process.argv[2] == '--run') {
var fn = process.env.LOCALAPPDATA + '/path/to/wibble.log';
var logStream = fs.createWriteStream (fn);
service.run (logStream, function () {
'use strict';
service.stop (0);
});
// rest of app here
}
module.exports = app
Hopefully I've overlooked something dumb. My gratitude if you can spot it!

Bluemix NodeJS ENOTFOUND

I have a bluemix node js application which communicates with a server. I have test and production environment. On the development environment we communicate to the test server, and I get a node js error.
When I change the server URL to the production server everything is ok.
When I run the app on localhost and connect to the test server everything is ok too.
So my problem is only on bluemix environment with communication to my company test server. Error is:
{
"code": "ENOTFOUND",
"errno": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "www.xxxxxxxxx.cz"
}
Hostname in error is masked.
From the exception, I think the failing code is doing a dns lookup. I wrote this sample code and found that the error is similar or same.
var dns = require('dns');
dns.lookup('non-existent server', function(e, a) {
console.log(e);
});
And the output is:
bash-4.1$ node h.js
{ [Error: getaddrinfo ENOTFOUND non-existent server]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'non-existent server' }
bash-4.1$
Problem determination steps would be:
ping your target server from a machine which has outbound access - to make sure the server is present. If not, resolve that problem.
Logon to bluemix debug console
ping your target server. If it does not respond, there is a wall between bluemix and the target. If it responds, try this test case. If that too works, we will have to debug further, I can be of further help.
Bluemix debug console is obtained through:
Export an environment variable "BLUEMIX_APP_MGMT_ENABLE" with value "shell"
Restage the app.
Login into the web shell in browser at https://your-app-url/bluemix-debug/shell/ using your Bluemix user credentials
Hope this helps.

Hubot windows service

I would like to wrap my Hubot in a windows service to deploy it.
I am using node-windows to do this but I'm having some trouble trying to get it to run a coffee-script file (which is what hubot uses).
If i manually run
> coffee .\node_modules\hubot\bin\hubot
everything works fine.
But I cant work out how to call this from a node-windows script. My attempt below:
var Service = require('node-windows').Service;
var svc = new Service({
name:'Hubot',
description: 'Hubot',
script: 'coffee .\\hubot\\node_modules\\hubot\\bin\\hubot'
});
svc.on('install',function(){
svc.start();
});
svc.install();
which fails with the following:
C:\Users\luke.mcgregor\hubot>node app.js fs.js:747 return
binding.mkdir(pathModule._makeLong(path),
^ Error: ENOENT, no such file or directory 'C:\Users\luke.mcgregor\hubot\coffee
.\hubot\node_modules\hubot\bin\daemon'
at Error (native)
at Object.fs.mkdirSync (fs.js:747:18)
at C:\Users\luke.mcgregor\AppData\Roaming\npm\node_modules\node-windows\lib\daemon.js:409:16
at FSReqWrap.cb [as oncomplete] (fs.js:226:19)
The script is not a command, only the path to a file. You can change the executable used by node-windows to run your scripts by setting an execPath environment variable: https://github.com/coreybutler/node-windows/issues/61#issuecomment-51423542

rest call from node fails

I am building a node.js/express app which makes remote calls to other internal web application (ASP.NET WEB API) to consume json from it. We are in a corporate network. Here is the strange issue.
On my Mac OSX (Mavericks), I can curl from shell and get json from http://our_internal_host:9991/connections. I can also type this URL and see the JSON response from the browser.
When I run this express app locally and request the route which makes the remote call, I see this error on the console. The route handler logs this message below and browser hangs.
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
Node process cannot make a connection to that address. I also have a Windows 7 machine at work and I do not encounter this issue on my Windows 7 machine. When I run the same node app on Windows 7, I don't have any issues.
I am not sure how to troubleshoot the issue...
PS: A colleague of mine who has the same setup doesn't have this issue. We compared DNS configs but our setup looks to be same.
Any pointers to troubleshoot this issue is much appreciated. I know this is environment specific issue but not sure where to start.
Thanks
EDIT #1
Route handler making the remote call which logs the error above...
var http = require('http');
var options = {
host: 'our_internal_host',
port: 9991,
path: '/analytics'
};
http.get(options, function(res) {
// removed response code
console.log(res);
}).on('error', function(e) {
console.log(e);
});
It turns out my Mac's IP was on a different subnet than other machines.

Resources