How do I see my node server?

October 9, 2018 3.1k views
Node.js Ubuntu 18.04

I’ve set up my app and it is running but after ssh-ing out of the server, now I don’t know how to get access to my node server again. This is a super basic operation but I can’t find the steps on how to do this anywhere.

To elaborate, the server is running and I want to see the logs in real time like how the node server runs in development. I am logged into my droplet but I see only the command line. What are the commands to display my node server?

2 Answers

It depends on what article you followed to setup and install nodejs. There’s the “foreground” method of running nodejs where it stays visible to you in the terminal. And like you said the log is displayed back. And then there’s the “background” method where it runs silently but still outputs messages in log files. I can point you to the log if you can provide the link you followed to install it.

  • Hi!
    I used this https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04. I followed all the prerequisite tutorials there, aside from the lets encrypt one, since that would not work.

    I guess I don’t really know how to run the app. I start the server and hit the endpoint place-puppy.com and it works. As soon as I close the terminal it kills my node server. I assumed it would keep running on the droplet even after I closed the terminal. So the problem seems to not to be that I cannot see the server running, but that it is not actually running at all.

    So, I guess I have an even more fundamental problem. How do I run my server? I want it to run forever unless I kill it. I have PM2 install but have no idea how to use it.

    • The tutorial shows you how to create a service with pm2. But if you want to run nodejs on the terminal until you kill regardless of whether you’re ssh'ed in to the server, you’ll need to run the program screen.

      So run these commands in this squence:

      1. Login to your server via SSH or console

      2. Type: screen

      3. Start your nodejs server

      4. Leave this terminal for as long as you want but if you want to regain control of it run: screen -d -r

      That’s it

      • Thanks. Are there docs for this somewhere? Also, if I am looking at my server, and I want to exit from it and just go back to my normal command line on my local machine, how can I do this without killing the server, or closing the terminal window?
        command-d sometimes works (mac) but often sends me to the dreaded [process-complete] prompt and then I am forced to close the window. (I hate closing the window :))

          • Great. I actually just googled it after you mentioned it and found a bunch of stuff as well. I am still having an issue where I can re-attach to a screen, the only one running with screen -ls but my sever does not appear. Any idea how I can see the server?
            The server is running somewhere, I just can’t find it :)

          • It definitely has a learning curve to it. But once you do it a few times it’s a a piece of cake. Try this:

            screen -d -r
            ps -elf|grep nodejs
            

            What do you see?

          • Yeah for sure!

            I get this
            0 S arssoni+ 8407 8327 0- 3714 pipe_w 19:28 pts/3 00:00:00 grep --color=auto nodejs

          • If running “screen -d -r” completed without error or warning that means you’re inside a SCREEN session. And because there’s no nodejs process running, go ahead and start nodejs now. Anytime you want to close your terminal but keep nodejs running you can do that and nodejs will keep running in the background. If you want to reconnect to the screen session after you close your terminal run this:

            screen -d -r
            

            If you want to get out of the screen session but keep it running, type this:

            ctrl-a-d
            
          • I was able to attach/detach and quit useless sessions using the commands you’ve given me, but I could not see the running server. When I try to restart node, I go node app.js in this case, it says that port 3000, the place I started it originally, is already in use.

            I guess I thought I would be able to log in to that particular running server?
            If I do screen -d -r 4445 , I think this is the name of the screen I want, it brings me there, but there is no running server.

          • Let’s figure out what’s running on that port. Do this:

            netstat -atnlp|grep “3000”

            fuser 3000/tcp

          • This is out of comfort zone now. Haha.
            netstat -atnlp|grep "3000"
            Gives:
            tcp6 0 0 :::3000 :::* LISTEN 4502/node

            And
            fuser 3000/tcp
            Gives:
            3000/tcp: 4502

            I killed the process and now my app is dead.
            So, when I started the app I used a npm script npm run prod. That is the script to start it. I’m a currently trying to get pm2 to run the app but it will not. Man this is confusing.

      • So the latest is I am trying to get pm2 to run it. I use the following script pm2 start npm --name=app-name -- run prod, but even though I am in the directory of my app, it looks for the packages.json in the root.
        I get this error

        npm ERR! enoent ENOENT: no such file or directory, open '/home/my-dir/package.json'
        

        Any idea how to make it run from the directory I am in? Thanks for all your help.

        • Seems to be working now. I finally found something that worked killall -s KILL node
          .
          I’m not sure what was going on. Even my npm script above worked now, from the same dir it would not before. Anyway, hopefully this means I can move forward now.
          Thanks

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!