listen() doesn't return? - node.js

I'm trying to get started with Node, and have hit a hump. When I try
to run the below (same example I've seen everywhere) the "starting..."
line executes, as well as "created.", but it seems like the script gets stuck on the next line, and never prints "started." (and the server doesn't work, tried via a
browser, telnet, curl, etc). Any advice on how to debug?
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
console.log("starting...");
var s = http.createServer(onRequest);
console.log("created.");
s.listen(8888);
console.log("started.");
Debian GNU/Linux 6.0.1 (squeeze), 2.6.39-x86_64
Compiled node.js 2011.07.19 v0.4.10 (stable) from source.
Thanks!

Actually it looks like listening has some problems with socket access. Try to change port number, turn off the firewall or run node with root access. Maybe it should help;
In other way try it with python or smth like that;

Change the port number to 3000 AND run as root. Ensure port 3000 is NOT being used (i.e. use netstat). You should see the "started" message. Otherwise, I can't really help. I'm running Node 0.4.10 on Ubuntu and I copied/pasted your code; it runs perfectly. If it still doesn't work, reinstall Node.

Related

Why does NodeJS download the file instead of holding the web

I am not sure about why there is a difference between Mac OS & Windows.
The problem is, on Windows environment, the Chrome/Edge downloads the NodeJS response instead of just loading the webpage. Whenever I visit "localhost:8081" on Chrome/Edge, it happens again and again. I would like to do it as what MAC OS does, anyone can save me?!
Windows Side Response
Windows
On MacOS, it does sticking on the browser window.
Any idea for this issue?
Forgot to mention NodeJs version, MACOS: v10.15.3, WINDOWS: v11.12.0
Hopefully, the version doesn't matter in this case. :'(
MacOS
Thanks!
var http = require('http');
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('HelloWorld');
}).listen(8081);
console.log('server running at http://127.0.0.1:8081/');

Node.js not working on Windows XP machine

I am just getting started with node.js and I have followed multiple tutorials to get it working on my Windows XP machine.
Downloaded the msi and installed with no issues, opened a command prompt and typed node -v and nothing displays. I created a folder and made a JS file with console.log('hello'); in it, changed to that directory and typed node hello.js and nothing shows.
I then added this code to the hello.js 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/');
and ran the file from the command line again, it paused briefly and then again displayed nothing.
Been through 10 different tutorials on 10 different sites and can't find a thing to answer this question, even all the information here was no help at this point.
I have also checked the path and it is fine and rebooted the machine just in case.
Seriously stuck!
You need to set your path to node (Environment Variable). Check the following address:
http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html
This is necessary for running node from any folder in the system.
Or you can add node as an enviroment variable in Windows XP's settings:
https://support.microsoft.com/en-us/kb/310519

No response on localhost from nodejs server

I have just begun with NodeJs and was trying a commonly found basic example of a server -
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
console.log('Hi there !');
Now when I run node example.js using cmd, I get the correct message Hi there !. However, when running on localhost:8888 or 127.0.0.1:8888, I get This webpage is not available.
I also have XAMPP installed but from what I know that uses port 80.
Also my installation directory and my code directory is different. Is that causing a problem here ? Any help appreciated

display html page with node.js

This is my first time with node.js. I get it to display the index.html, but it doesn't display the images on the site or anything else, it ONLY shows the basic html stuff. Here's how I set it up.
There's no apache, php or anything else on the server, just ubuntu, proftp and node(and curl and the other dependencies). I made the main directory for the node files /var/nodeFiles and the directory for the html/site files is /var/nodeFiles/www
so for my node server file I did it like this:
var http = require('http'),
fs = require('fs');
fs.readFile('/var/nodeFiles/www/index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(80);
});
this works, but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display. The files and directories are all correct, I've double checked and the permissions of the folders are correct. So what else do I have to do to get node to display the rest of the site?
I hope I've explained my self correctly, I was told this is the place to ask development questions.
Thank you for taking the time to read this.
but it ONLY shows the index.html file and NOTHING attached to it, so no images,
no effects or anything that the html file should display.
That's because in your program that's the only thing that you return to the browser regardless of what the request looks like.
You can take a look at a more complete example that will return the correct files for the most common web pages (HTML, JPG, CSS, JS) in here https://gist.github.com/hectorcorrea/2573391
Also, take a look at this blog post that I wrote on how to get started with node. I think it might clarify a few things for you: http://hectorcorrea.com/blog/introduction-to-node-js
Check this basic code to setup html server. its work for me.
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
This did the trick for me:
var express = require('express'),
app = express();
app.use('/', express.static(__dirname + '/'));
app.listen(8080);
If your goal is to simply display some static files you can use the Connect package. I have had some success (I'm still pretty new to NodeJS myself), using it and the twitter bootstrap API in combination.
at the command line
:\> cd <path you wish your server to reside>
:\> npm install connect
Then in a file (I named) Server.js
var connect = require('connect'),
http = require('http');
connect()
.use(connect.static('<pathyouwishtoserve>'))
.use(connect.directory('<pathyouwishtoserve>'))
.listen(8080);
Finally
:\>node Server.js
Caveats:
If you don't want to display the directory contents, exclude the .use(connect.directory line.
So I created a folder called "server" placed index.html in the folder and the bootstrap API in the same folder. Then when you access the computers IP:8080 it's automagically going to use the index.html file.
If you want to use port 80 (so just going to http://, and you don't have to type in :8080 or some other port). you'll need to start node with sudo, I'm not sure of the security implications but if you're just using it for an internal network, I don't personally think it's a big deal. Exposing to the outside world is another story.
Update 1/28/2014:
I haven't had to do the following on my latest versions of things, so try it out like above first, if it doesn't work (and you read the errors complaining it can't find nodejs), go ahead and possibly try the below.
End Update
Additionally when running in ubuntu I ran into a problem using nodejs as the name (with NPM), if you're having this problem, I recommend using an alias or something to "rename" nodejs to node.
Commands I used (for better or worse):
Create a new file called node
:\>gedit /usr/local/bin/node
#!/bin/bash
exec /nodejs "$#"
sudo chmod -x /usr/local/bin/node
That ought to make
node Server.js
work just fine
You can simply use
res.senFile('PATH_TO_FILE');

Why does Cloud9 IDE return the error "Script does not exist" when trying to run simple Node.js script?

I am attempting to create a simple Hello World Node.js script using Cloud9 IDE. My code is below and is the atypical 6 lines:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT);
I don't see any errors and the IDE is certainly not alerting me to any. I've ensured that the file is saved (even going so far as to commit it to my repo). Unfortunately, whenever I try to run the above code I receive the following error:
Error on server
Received following error from server:
"Script does not exist: server.js"
To illustrate the run configuration I've included the below screenshot:
I am pretty sure I am missing something obvious, but whatever it is its alluding me. What do I have misconfigured that could cause my server.js file to not run?
Your configuration is correct, there is however a temporarily problem that causes some of the VM's to not run a file. I've seen it before and it usually goes away after some time. Quick fixes now:
Either wait
Create a new project with the same file, chances are that it'll run
Your filepath doesn't look right. Shouldn't it be server.js and not /server.js? Try creating a new run configuration and set the file path to server.js.

Resources