I am experimenting with a simple node app...
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/');
I am able to browse to host:1337/ and I see 'Hello World'. However, if I change the port to 3000, I can't load the page.
You might have another service running at port 3000.
type
netstat -a -b
on the command prompt
it will list all listening ports.. check if 3000 is in use.
I changed the console log, but not the actual port number in the first argument of the listen method in this line...
}).listen(3000, "127.0.0.1");
...move along...nothing to see here! ;)
Related
here is my codeI have typed in this code and in the browser showing error
const http = require('http');
const server = http.createServer((req , res) =>{
res.end('Hello fromm the server!');
});
server.listen(8000, '127.0.0.1', () => {
console.log('Listening to request on port 8000');
})
I'm not able to recreate this issue. Have you executed your node script in your terminal?
In this example, I've name the file 'index.js'
node index.js
Your terminal should show 'Listening to request on port 8000' after you execute your node script.
I am trying to follow tutorial on how to use socket.io with express.js framework and node.js.
Every tutorial I am following suggested I use the following lines to establish a connection in app.js
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3000);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
})
This worked if I use port other than 3000 which I am having to run my application on http://localhost:3000/. I get the error that Port 3000 already in use.
After debugging and looking at the code I think I have an idea of why is this happening. In ./bin/www.js file (created automatically by express js) we have the following lines:
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
but I am not sure how to reuse this created server with same port in my app.js. I am totally new to node.js. How do I setup socket io on the express framework the right way?
hello there please put your server.listen after socket connection like this
var app=require('expess')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
})
server.listen(3000);
I hope this would work.Thanks
In order to kill any existing node process, you can run killall node command in your shell.
Remove this line from your code, which is hardcoding the port number -
server.listen(3000);
and add something like this instead -
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'));
So your code would look something like -
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// server.listen(3000);
app.set('port', process.env.PORT || 3000);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
});
server.listen(app.get('port'));
Now when your run your app again, it would boot on port 3000 by default or you can pass an environment variable (PORT) while starting the server like this to run on other ports.
$ PORT=8080 node app.js
hello I'm using docker and ansible to launch a container which will then launch a server.js file, which needs to display the contents of an index.html file located in the same directory. I have this skeleton outline code which works in displaying "Hello World" to the screen when I curl the ip address running this server.js file
var http = require('http');
http.createServer(function (request, response)
{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
the console.log does not display on the screen, seemingly only response.end does that, with printing 'Hello World' to the screen, so I've been trying to read in the index.html file as a variable and have response.end display it but with no luck.
I tried doing this:
var http = require('http');
http.createServer(function (request, response)
{
response.writeHead(200, {'Content-Type': 'text/plain'});
var fs = require('fs');
var readStream = fs.createReadStream('index.html');
readStream.pipe(response);
//response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
but when I tried to curl it it resulted in a an Error 52 empty reply from the server, am I not doing enough to read in my index.html file and store it as a string variable? thanks.
var http = require('http');
var fs = require("fs");
http.createServer(function(req, res) {
fs.readFile("index.html","utf8" ,function(err, contents){
console.log(contents);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(contents);
res.end();
});
}).listen(3000);
if you want to show the contents of the file on request to the server try this.
I'm very new for this stuff, and trying to make some express app
var express = require('express');
var app = express();
app.listen(3000, function(err) {
if(err){
console.log(err);
} else {
console.log("listen:3000");
}
});
//something useful
app.get('*', function(req, res) {
res.status(200).send('ok')
});
When I start the server with the command:
node server.js
everything goes fine.
I see on the console
listen:3000
and when I try
curl http://localhost:3000
I see 'ok'.
When I try
telnet localhost
I see
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'
but when I try
netstat -na | grep :3000
I see
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
The question is: why does it listen all interfaces instead of only localhost?
The OS is linux mint 17 without any whistles.
If you don't specify host while calling app.listen, server will run on all interfaces available i.e on 0.0.0.0
You can bind the IP address using the following code
app.listen(3000, '127.0.0.1');
If you want to run server in all interface use the following code
app.listen(3000, '0.0.0.0');
or
app.listen(3000)
From the documentation: app.listen(port, [hostname], [backlog], [callback])
Binds and listens for connections on the specified host and port. This method is identical to Node’s http.Server.listen().
var express = require('express');
var app = express();
app.listen(3000, '0.0.0.0');
document: app.listen([port[, host[, backlog]]][, callback])
example:
const express = require('express');
const app = express();
app.listen('9000','0.0.0.0',()=>{
console.log("server is listening on 9000 port");
})
Note: 0.0.0.0 to be given as host in order to access from outside interface
I'm trying to execute a javascript code with node.js, and I get always two errors saying :
.port 1 is not active
.port 2 is not active
This my javascript code :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Any ideas ?
Do you have anything else listening on 8124 already?
netstat -an|grep :8124