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
Related
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:
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.
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.
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.
I'm running node.js v0.11 on Android (via https://github.com/paddybyers/node). I attempted to try out the "Hello HTTP" example found here: http://howtonode.org/hello-node, however, I ran into problems.
The server starts fine, but as soon as I attempt to connect to the http server (by visiting http://localhost:8000/), I get this error:
net.js:1156
COUNTER_NET_SERVER_CONNECTION(socket);
^
ReferenceError: COUNTER_NET_SERVER_CONNECTION is not defined
at TCP.onconnection (net.js:1156:3)
My code is exactly the same as the Hello HTTP example:
//Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
How can I fix this?
Thanks!
Compile Node.js with the HAVE_PERFCTR option. (You Need to implement your own perfctrs for Android. See node_counters.cc line 130).
It should be also possible to define the missing functions in your script as empty functions.
PS.:
You also need DTRACE.
I simply commented out all COUNTER_NET_, COUNTER_HTTP_, DTRACE_NET_, and DTRACE_HTTP_ calls in the javascript files under lib/. This amounted to about 10 or so lines that I commented out of net.js and http.js.
I think that js2c.py is supposed to process src/macros.py and src/perfctr_macros.py to effectively do this 'commenting out' for you when it serializes the scripts under lib/ to C arrays in out/release/src/node_natives.h, but that doesn't seem to be happening.