im making a mobile app that require a http server so im trying to make it with browserify so this is what im doing, i got this server.js
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/');
then i do this
$ browserify server.js > browserify.js
then i enmbeded to my html like this
<html>
<body>
<script src="https.js"></script>
</body>
</html>
then when i run the html i get this error
Uncaught TypeError: http.createServer is not a function
how can i get a http server working on the browser ?
by the way i got http-browserify installed
Apologies if I'm reading this question wrong, but you can't create an http server from within a browser. That http.createServer call is only meant to be called (and is only defined) in node.js.
So you would need to run your script from node from the command line, like this:
node https.js
Related
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 am trying to run two node.js application in my development machine but the second application throwing the following exception while running.
Is there any way to run these two applications in parallel way ?
You need to use a different port since 3000 is already in use.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3001, "127.0.0.1");
Address is already in use, you should change the port from for example 3000 to 3001 for the second instance of the script.
You can't bind two sockets on the same port. Hence the error.
The usual good practice is to rely on the PORT environment variable so as to be able to quickly change the listening port from the command line.
var http = require('http');
var port = process.env.PORT || 3000;
http.createServer().listen(port);
Then launch your application:
$ PORT=8080 node app.js
See here for cross-platform instructions on how to define environment variables from the command line.
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
I'm building an application that links to a node.js hosted on my home computer (78.233.79.103:8000)
The server is properly istalled (if you go to the addres I gave you'll se the socket.io works)
If I run the server in localhost with
node server.js
all is good, the application works
Then when I run the application on my other pc or on my iPad (I wrapped it with phoneGap so it's just a web app included in a native iOS app), trying to connect the io on 78.233.79.103:8000 i got the console log:
io is not defined
here is my sourcecode: https://github.com/synbioz/puissance4
look for the server.js
I know I only call io = require('socket.io').listen(PORT); but kwnow also I should create something like:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, '78.233.79.103');
that actually doesn't works
Any idea?
probably because .listen() doesn't return this.
try writing them in separate lines.
var io = require('socket.io');
io.listen(PORT);