Nodejs webserver use script port 3000 - javascript

I created a script in nodejs that extract data from database and create a file with all db data. I create also a webserver node js listening on port 3000 and with forever is working in listening mode all time. But now the script is without web interface and use a command line prompt and other modules. How remote users can use a remote script like : node scrip.js like a command shell prompt on locale machine ?
This code working but in-globe only hello world not my prompt command and db retrieve info. Thanks
var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');

I'm not completely sure I understand what you are asking. But if you are just asking how to communicate with a local server running on port 3000.
Then if this is a GET request, you could simply type 'http://localhost:3000/' in a browser, or you could use a tool like postman, I recommend the Chrome app.

Local server is listening on port 3000, but i can see only hello world. But when one user access to my url http://ip:3000/ needs to use a command shell like >
node script.js because i have different operations db retrieve information and input user data from command line input.

Related

Why is NodeJS only showing files on my browser instead of the results of my code?

I'm new into node JS. I'm trying to install node and run just a 'Hello World' app but I have problems with my server.
When I try to run my app, the server is showing only the index files and not 'Hello World'. enter image description here
The node server is working and says that the http-server is available.
My code for my Hello World app:
var http = require("http");
http.createServer(function(req, res){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://192.168.178.14:8080/');
I want to run the file 'index.js' (second file in first screenshot). How can I fix this?
As Mikael Lennholm and robertklep have mentioned in the comments, the issue is that you are trying to run a second server on the same address (:8080).
I tested this myself to make sure, and here's the results I got:
Running an instance of the ecstatic server on :8080, producing a very similar image to yours (using default code provided by ecstatic's documentation).
Attempting to run a regular server on the :8080 port gives me an error, EADDRINUSE :::8080 (which is expected, as the other application is already using that address). You should probably get this error when trying to run your application as well.
Now, either you or someone else is to blame. But the fact is that there is already a server running on port 8080. I'd reccomend you try to find out who set it up (if it wasn't yourself) or maybe just try using another port. For example, 8088:

Unable to get Node.js hello world application working [Fedora 21]

I'm trying to write a node.js application, trying to make sure my environment set up first.
The webserver is Fedora 21, and I have opened port 3000 on the firewall and flushed ip tables. nmap localhost indicates that port 3000 is listening.
I'm using this node application
var http = require('http');
var PORT = process.env.PORT || 3000;
http.createServer(function (req, res) {
console.log('%d request received', process.pid);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world!\n');
}).listen(PORT);
console.log('%d listening on %d', process.pid, PORT);
But when I navigate the browser to my server I don't get any hello world message like intended.
Perhaps check for firewall rules? IIRC many firewalls prefer to drop packets instead of actively rejecting -- so you may find that (eventually) the web browser times out.
Easy way to check:
In one console, do: nc -l -p {whatever your PORT environment variable is set to, or 3000, as per your server logic}
and in another console, do
echo hello | nc localhost 9999
If you see the hello in the first console, you can at least rule out some tcp oddness / firewall issue.
Conclusion I'm an idiot who needs another cup of coffee.
it works 100% just instead of navigating to hostname/webapp I need to navigate to hostname:3000

How to run a website developed with node.js in local?

I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on nodejs.org:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.

Node JS - Server doesn't react to requests

I set up a Node JS server, and made a request to it, it just loads and loads and eventually says "Server not found". Here is the code for my file:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
When going to externalIP:1337, the phenomenon described above happens. I am running Ubuntu 14.04, node JS version 0.10.32.
What is going on?
You're specifically listening to 127.0.0.1 which is localhost. If you want to allow connection via the external IP, you should omit the '127.0.0.1' argument in your listen. i.e. change listen(1337, '127.0.0.1') to listen(1337). Otherwise go to localhost:1337 instead.
The problem is that you're only listening for requests on localhost. If you try to access the server from outside the system you won't get there because the server isn't listening on a LAN IP.
Change
.listen(1337, '127.0.0.1');
to
.listen(1337);
That will listen on all available network interfaces on the system. You could specify a LAN IP (just like you did for localhost) if you wanted to listen on a specific network interface.
Sorry.
Apparently tomcat was also using port 80. So by disabling tomcat I got it to work.
Thanks.

Node.js Unable to load the webpage The connection has timed out

I'm a beginner to node.js, I installed it as here:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
I tried it from console and it worked:
console.log('Hello World!');
nodejs helloConsole.js
> Hello World!
Then I tried to make it within HTTP Server, here is the code:
var http = require("http");
http.createServer(function (request, response) {
   request.on("end", function () {
      response.writeHead(200, {
         'Content-Type': 'text/plain'
      });
      response.end('Hello HTTP!');
   });
}).listen(8080);
I run it from terminal:
nodejs hello.js
then I open the browser at http://localhost:8080/ , it takes a long time to load, then at chrome it gives me that:
Unable to load the webpage because the server sent no data.
and firefox gives me that:
The connection has timed out
Note : other web servers works fine, like apache.
You are listening to the "end" event of the request parameter, but at the time your outermost callback function is called, the request has already ended, so it is too late to subscribe to that event.
You can directly respond from the outermost callback, which is what the sample code in nodejs.org shows.
You attached a listener to the request object, listening for the end event, you will not see a response until that event has been caught by request.
To test things out you might want to modify it like the one provided as example here: http://nodejs.org/
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Try the following steps, if you are using windows, it may help you.
Download the node.exe from http://nodejs.org/download/
Place in a Hard Disk Drive from where you want to run like D:/nodejs/node.exe
Now place your hello.js in the same directory it should look like D:/nodejs/hello.js
Now from Command Prompt Go to that folder D:/nodejs/ and run command node hello.js

Categories

Resources