How to start Node.js in specific URL? - javascript

When I'm updating (and testing) a specific page in my application, I need to stop the server, start again with node server.js, switch to the browser window, hit F5 and switch back to the terminal to see the output. This takes a lot of time.. :)
Is there a way I can start Node with something like this:
node server.js -url /my_page
so I can directly see the output as if someone hit the page from their browser?
I found this question but I guess it's included in the server code so it needs to be updated each time too. It is a solution, but I wonder if there is a faster way to do this.
Thanks,
Edit:
With curl, I'm getting this error:
$ node server.js & curl 'http://localhost:5000/my_page'
[2] 12824
curl: (7) Failed to connect to localhost port 5000: Connection refused
[1] Terminated node server.js
$ Listening on port 5000
Notice the last line, is it executing curl before the server is started?

node server.js & curl 'http://localhost/my_page'
This starts the server in the background, then immediately uses curl to make an HTTP request.
The server will still be running after this. You can bring it to the foreground (e.g. in order to kill it) with fg. Alternatively just run the two commands (without &) in separate terminals (or screens).
If the server doesn't start fast enough, just add a delay:
node server.js & sleep 1 ; curl 'http://localhost/my_page'

Related

How to make sure expressjs start correctly at first run before running it forever

I have a server using expressjs and the forever package (https://www.npmjs.com/package/forever) to keep the server always running.
I have a problem: when the server starts, it should be off if there is any config wrong, but the forever package always restarts it. How to make sure the server starts correctly (can be off at first run) and run forever if the config is right?
The config I mentioned is gotten from AWS

express.js server starts on a port that is being used

Have this setup:
expressjs server started from one node process, listening to port 8081.
js client trying to make a request to it from another node process, using node's http module.
client failing with 400 error.
same URL (http://localhost:8081/) is opening in browser just fine.
Spent a few hours trying to troubleshoot it, then tried to change the port and it worked.
Turns out there is another process listening on port 8081:
$ lsof -i tcp:8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
EUSAManag 1187 oleksandr.suhak 4u IPv4 0xce3bb9546cff3ab1 0t0 TCP localhost:sunproxyadmin (LISTEN)
(Have no idea what EUSAManag is)
I guess my question is: how could it be that the express server starts fine without complaining to a "port being used by another process" when the port is clearly in use. And why does it work then when accessing it from the browser, but does not work when making request from js client? Any tips on figuring out what is actually happening here?
If guess that EUSAManager.exe come from one of software https://iit.com.ua/downloads which is in the business of information security. Most likely their software interfere too much with you operation system.

get error listen EADDRINUSE :::8080 after first successful server start

Hi this problem just currently started happening and have never had this issue before. Every time I start up my express server it runs fine the first time. When I close the server in my git bash with control C it shuts down but when I try to start up the server again I get the EADDRINUSE error. This makes no sense at all and I have tried to use killall -9 node command without any success does anyone have any suggestions as to what might be going on.
EADDRINUSE error means that the port that your server is trying to use is already being used by something else. My suggestion would be to close all terminal windows first. Then start a completely new terminal window, and then try running the server. If that does not work, restart your laptop(I know this is tedious, but this usually works), and follow the above steps.

Node.js - Server crashes when started on startup, doesn't crash when manually started

I am running Node.js and Socket.io for online chat.
I have created a file in:
/etc/init/example.conf
it has two lines:
start on startup
exec forever start /var/www/example.com/html/server.js //location of server file.
Whenever I start file upload in chat application, it crashes but instantly restarts.
Whenever I kill node process though, and start it manually - it works fine.
I also can't get any logs or anything from terminal as when it's auto started - it doesn't print me anything to terminal.
I am still new to Node.js and Linux in general.
Node.js is running on Express + Jade.
How do I determine specific cause?
I managed to solve my problem, after a bit of searching around I found out about tail command.
My issue was a bit harder to trace because Node.js was a process started by autostart so when I launched terminal and connected to server, process was just running in background basically and I wouldn't get any output (including exception messages).
Anyway, solution that worked for me was:
I typed
ps aux | grep node //to find PID of node process
then I went to following directory
cd /proc/[pid of running node service]/fd
In fd directory there are few objects you can get output from but if you want to attach and listen to servers output including uncaught exceptions, you need 1.
So:
tail -f 1
that way I was able to cause website to crash and see the output.

Sails.js server not starting anymore

I am using Cloud 9 IDE to develop a simple CRUD application using Sails.js (node.js MVC framework). Up until today I had no trouble starting the Sails.js server.
Today, I've been trying to start the sails js server, but I keep getting this error:
warn: error raised: Error: listen EACCES
error: Server doesn't seem to be starting.
error: Perhaps something else is already running on port 8080?
I have checked my /config/local.js file and everything is just fine, as it should be. The port is set to process.env.PORT || 1337 so it shouldn't have any problems firing the server up.
I'm looking forward to your insight.
Thank you!
Open terminal and run this command:
$ lsof -i :8080
Output will show PID of process occupying port 8080: "httpd 1234 ....'
Then kill the process with this command
$ kill -9 1234
Sails will now run
Hmm-- looks like port 8080 isn't available. What happens if you try to switch the port? You may have another server running on that port. Or in some cases, hosts require the hostname to be set. I'd try switching the port first though.
The only real answer to this is: wait. C9 seems to kill servers in a weird way that causes Sails to jack up and blocks you from establishing another server. lsof -i doesn't show anything serving... but it still won't start. Seems to be an issue with Cloud 9 and Sails.js. If I serve a generic Node.js "Hello World" app on the same port, the issue doesn't occur. However, time, it seems, cures all. After awhile, Sails seems to snap out of it and starts serving again when lifted.
Incredibly weird.

Categories

Resources