I am starting two different node servers, on different ports, but I still get the following error.
info - socket.io started
info - FlashPolicyFileServer received an error event:
listen EADDRINUSE
This is how I am starting the first server:
"use strict";
var
express = require('express'),
app = module.exports = express();
// set some config vars
var
server = require('http').createServer(app),
socket = require('./app/lib/socket');
// these settings are common to both environments
app.configure(function () {
// configuration left out
app.use(app.router);
});
// Load the routing
require('./app/routes')(app);
// run the server with socket.io
server.listen(3001);
socket.listen(server, session, app);
I am starting the second server the exact same way except the second last line is changed to :
server.listen(3002);
socket.io is started like this in another file
exports.listen = function (server, sessionStore, app) {
var io = require('socket.io').listen(server);
...
Not sure how to fix this error.
The flash policy port defaults to 10843, so both apps will try to run it off this port, which is the error you are getting. Either remove the transport, or set the port using
io.set('flash policy port', 3005)
Or you can just remove that transport altogether:
io.set('transports', [
'websocket',
'xhr-polling',
'htmlfile',
'jsonp-polling'
]);
Related
I am writing a RESTful API. IT runs on node.js using the express.js framework, mongodb using mongoose as the object modelling tool & body-parser to pass the http. Everytime I start the server & navigate to the specified IP address, I get a "CANNOT GET/" error. How can I can around this? Some advice would be much appreciated .
I have tired using a different port number but the problem still persists.
Here is a copy of my server.js code:
var express = require('express'),
app = express(),
IP = process.env.IP,
port = process.env.PORT || 8080 ,
mongoose = require('mongoose'),
tasks = require('./api/models/todosModel'),
bodyParser = require('body-parser');
//handiling of promise
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Todosdb',{ useNewUrlParser: true });
app.use(bodyParser.urlencoded({extended:true})); // telling the sever instance to use body parser
app.use(bodyParser.json());
var Routes = require('./api/routes/todoRoutes');
//passing the server instance to the routes
Routes(app);
app.listen(port,IP);
console.log("The TODO API server is running on IP: " + IP + " and port: " + port);
The todoRoute code :
'use strict';
module.exports = function(app){
var todofunctions = require('../controllers/todoController');
// todo routes
app.route('/tasks') //task [GET (all the tasks),POST]
.get(todofunctions.listTasks)
.post(todofunctions.createTask);
app.route('/tasks/:taskId') //a task [GET(single task),PUT,DELETE]
.put(todofunctions.updatetask)
.get(todofunctions.readTask)
.delete(todofunctions.deleteTask);
};
It's probably because you have not defined any handler for /.
Try going to the /tasks instead in your browser, then you will get some response.
I am getting this error:
WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I have looked for this error but it seems for me that my configuration of the sockets is well and I do not think is for the warning of electron.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var JSONTCPSOCKET = require('json-tcp-socket');
var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
require("./rabbit")(io, JSONTCPSOCKET);
var socket = io('http://localhost:8000',{transports: ['websocket',
'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']});
Any idea?
Thanks mates!!
It was a silly mistake.
The file that contains the server that listen in that port is in my case server.js.
When you run it with node, the start file is server.js but when you run it with electron the start file is main.js and I was never running server.js when I executed with electron, so I was not listening in that port.
I'm running a simple nodejs server on my localhost on port :3434
const cors = require('cors');
const app = require('express')();
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.get('/ping/:anystring', (req, res) => {
console.log(req.params['anystring']);
res.send({
anystring: req.params['anystring']
})
});
app.listen(3434);
and I'd like to perform some ajax call from a website of mine.
I tried to configure the router port forwarding like so:
- name service: mylocalserver
- porta ragnge: 3434
- local ip: 192.168.1.19
- local port: 3434
- protocol: BOTH
but when I do
fetch(publicIP:3434/ping/hello).then(res => {
console.log(res);
})
I get error 404
Might anyone help me telling what I'm doing wrong?
You can't access your localhost server outside of your LAN unless you create a tunnel. I use ngrok.
There is an npm package for ngrok, but I couldn't get that one working, so I just manually start the server from terminal whenever I need to test an API.
Also you'll need http.
add this to your app.js:
const http = require('http');
const newPort = //your port here (needs to be a different port than the port your app is currently using)
const server = http.createServer(function(req, res) {
console.log(req); //code to handle requests to newPort
res.end('Hello World);
});
app.listen(newPort, function() {
console.log(`ngrok listening on ${newPort}`);
});
Now in terminal, after installing ngrok, use this ngrok http newPort where newPort = your port
You can view requests sent to your server by going to localhost:4040 (this might change depending on your system)
To send a request to your localhost, do this:
- name service: mylocalserver //not sure
- porta ragnge: ???
- local ip: //ngrok gives you a url in terminal when you start the server (I'm not sure if you can reference an IP)
- local port: newPort
- protocol: http //(ngrok gives you a different url for http and https)
You can use local tunnel
It maps your port on the localhost to a web url whithout the need to change your code
In my app.js file, I have the following code
var express = require('express');
var app = express();
var port = 8080;
var util = require('util');
var router = require('./base/js/routes.js');
//==================================================================
app.use('/', router);
// start the server
app.listen(port, function(request, response) {
console.log('Port 8080: Server Begins');
});
//==================================================================
var ipaddress = '123.456.789';
//==================================================================
var mongoose = require('mongoose');
var mongoURI = "mongodb://"+ ipaddress +":27017/test";
var MongoDB = mongoose.connect(mongoURI);
MongoDB.on('error', function(err) {
console.log(err.message);
});
MongoDB.once('open', function() {
console.log("mongodb connection open");
});
//==================================================================
The line var MongoDB = mongoose.connect(mongoURI);
is causing nodeJS not to work. I do not know why. NodeJS is on port 8080 and MongoDB is on port 27017.
I am fairly certain I installed mongodb package (and opened the port correctly). I just do not understand why nodeJS doesnt work when i include that connection line.
Side Note: Also I have the package forever installed: forever start -c nodemon app.js for nodeJS. If that is any relevance.
You are using wrong IP address format.
First try to connect with your local mongoDB instance if it work then you to check the IP address your trying to connect is correct or not.
Add the correct error message if problem still remain same.
change your mongod.conf file from /etc folder
In mongod.conf you need to change bindIp
If connection is local then set bindIp as
bindIp = 127.0.0.1
and if you want to use remote database then change bindIp as
bindIp = 0.0.0.0
then restart mongo service
hope this helps...
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