Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been practicing with "hello world" examples of websockets and node.js server. According to all those examples you create a html file (client) and a js file (server). Before you run them, you have to run this on the command line (I use windows)

 node nameOFtheServer.js

So, my question. If I close the command line window and open it again the client does not connect to the server. I have to run again the above code in the command line , manually, so the server will start again. Why is this happening? Is that normal? How can I fix it , so I dont have to run the same commands over and over again on the command line in order to start the js file (server) ?

Thanks

EDIT

OK, new facts, I just edited the question, highlighting the changes in Italics

share|improve this question
2  
You should post some code –  iandotkelly Jul 13 '13 at 19:00
    
Not normal, and we need to see your code to know why it isn't working –  Chad Jul 13 '13 at 19:00
    
Oh well, I'm off ....... for future reference, showing us how you run the .js file is rather less useful than posting the contents of the javascript and the html (or as small a test example as you can make) –  iandotkelly Jul 13 '13 at 19:11
    
@iandotkelly I have two files. helloserver.js has the code from this link and helloclient.html has the code from this link .The only difference is that I change the port to listen to 1337. Hmm...maybe is somethong about the port? I have to "activate" it? –  slevin Jul 13 '13 at 19:14
    
@iandotkelly Apparently, I did not understand what you asked me. I run the js file by simply typing node helloserver.js. Sorry, i did not get you... –  slevin Jul 13 '13 at 19:22

2 Answers 2

up vote 3 down vote accepted

When you close the terminal, everything that runs in it is killed. There are many solutions on both Linux and Windows systems, most of them create some sort of a service which then runs in the background.

Here are some possible solutions:

Pick the one that is best for you.

Related question on StackOverflow:

share|improve this answer
    
Thanks. Can you name a solution(s) for Windows? –  slevin Jul 14 '13 at 9:14
    
@slevin I edited my answer to include some possible solutions for you. –  Venemo Jul 14 '13 at 12:55

First of all thanks Venemo for your anser. I tried use the forever module, but did not worked well, as you can see here.

So I decided to use nssm with node.js

I download the nssm and unzip it in the C:Program Files\path\to\nodejs. And then I opened Window's command window and typed C:\program files\path\to\nssm-2.16\win32 and then typed nssm.exe. You should see a "menu" how to install or remove services. And now type

"C:\Program Files\path\to\nssm.exe" install give-Your-Service-A-Name "C:\path\to\node.exe" \"C:Program Files\nodejs\path\to\yourServerFile.js"

Notice the \ before the "C:Program Files\nodejs\path\to\yourServerFile.js" it's not a typo, you should type it, is important, if you have spaces in your path, helps nssm to interpret correctly.

And that's it, now press CTRL+ALT+DEL, open the Services tab, and find give-Your-Service-A-Name , right click and select Start service. To check, open your client file that communicates with the yourServerFile.js, it should be working, without having to start the yourServerFile.js from command line.

(PS : I use nodejs 0.10.12 and nssm 2.16 on windows 7. The instructions above are a combination of this tutorial and this anser)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.