Rilcon42
Rilcon42

Reputation: 9763

Windows Node Service not listening on port

I am trying to write a node service that listens on a port and writes to the Event log, but I can't connect when I launch a browser at http://localhost:41414/?x=y Im not getting event records from the listener file (which launches the express server). Any idea what I'm doing wrong?

I created a service using node-windows: launch using (node mysvc.js --install 42424)

var Service = require('node-windows').Service;

//-----------------------------------------------init vars
// Create a new service object
var svc = new Service({
  name:'My Listener v1.1',
  script: require('path').join(__dirname,'listener.js '+port)
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});
// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall',function(){
  console.log('Uninstall complete.');
  console.log('The service exists: ',svc.exists);
});
//-------------------------------------------------------end init vars
var port=0;
if(process.argv[2]=="--uninstall")
  svc.uninstall();
else if(process.argv[2]=="--install")
  if(parseInt(process.argv[3])>0){
    port=parseInt(process.argv[3])
    svc.install();
  }
else{
  console.log('must pass --install or --uninstall flag')
  return;
}

listener.js starts listening on the port:

const express = require('express');
var EventLogger = require('node-windows').EventLogger;
var log = new EventLogger('My Listener');

port=41414

app.get('/data', (req, res) => {
  const jdata = req.params[0];
  log.info(jdata+' WOOOOO!!!!');
})

log.log(`Service listening on port ${port}`);
app.listen(port);

Upvotes: 0

Views: 403

Answers (1)

syns
syns

Reputation: 51

you just missing this line in your listener.js

const app = express()

Upvotes: 1

Related Questions