Express and Cluster node modules in the same app - node.js

I am creating an app that allows users to upload files in their browsers to a server.
I started it by typing express in the folder of my project. With that express created all the folders and such. It created a bin folder containing a www file that starts a lot of stuff for us like the ports.
At first i only had one core so i was doing it like so:
var express = require('express');
var multer = require('multer');
var app = express();
var done = false;
var fileSize = 0;
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename){
return filename;
},
onFileUploadStart: function(file){
console.log(file.originalname + ' is starting...');
},
onFileUploadComplete: function(file){
console.log(file.fieldname + ' uploaded to ' + file.path);
done = true;
},
onFileUploadData: function(file, data){
fileSize += data.length;
console.log("FileUpload " + fileSize);
}
}));
app.get('/', function(request, response){
response.sendFile(__dirname + '/index.html');
});
app.post('/upload/', function(request, response){
console.log(request.body);
if (done == true)
response.end('file uploaded');
});
module.exports = app;
Eventually i needed to user a machine with several cores in order to be able to respond to more client requests. So I am trying to use the cluster module.
I changed the code accordingly:
var cluster = require('cluster');
if(cluster.isMaster){
var numCPUS = require('os').cpus().length;
for(var i=0; i<numCPUS; i++)
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
cluster.on('online', function(worker) {
console.log('worker ' + worker.process.pid + ' started');
});
}else{
var express = require('express');
var multer = require('multer');
var app = express();
var done = false;
var fileSize = 0;
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename){
return filename;
},
onFileUploadStart: function(file){
console.log(file.originalname + ' is starting...');
},
onFileUploadComplete: function(file){
console.log(file.fieldname + ' uploaded to ' + file.path);
done = true;
},
onFileUploadData: function(file, data){
fileSize += data.length;
console.log("FileUpload " + fileSize);
}
}));
app.get('/', function(request, response){
response.sendFile(__dirname + '/index.html');
});
app.post('/upload/', function(request, response){
console.log(request.body);
if (done == true)
response.end('file uploaded');
});
module.exports = app;
}
The problem is that when i do this i am getting the following error in the console:
Run: node ./bin/www
Result:
app.set('port', port);
TypeERror: Cannot call method 'set' of undefined at Object.<anonymous>
This happens since i placed the entire code inside the forked childs of the master. Anyone knows why this happens and how i can fix it?
EDIT:
In here you can see the bin/www file where the port and some other configurations are set:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('dropbox:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

Related

Sockets.io issue: WebSocket is closed before the connection is established

I am currently building a nodejs webrtc video conference, hosted on azure. The program works perfectly fine locally, but when hosted there seems to be an issue with websockets. The client side error is below:
WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established.
index.js:83 WebSocket connection to 'wss://etuition.azurewebsites.net:8080/socket.io/?EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established.
I have seen other stack overflow posts claiming that this is due to SSL, but site is currently running on HTTPS so this should not be a problem for me. Is it possible that the ws server is insecure even though my http server is secure?
Any other advice on what could be the problem will be greatly appreciated.
Below I have included my server.js code.
Please ask if any more information is needed:
/**
* Server module.
*
*
*/
'use strict';
var environment = process.env.RTC_ENV || 'local';
var debug = require('debug')('expressapp:server');
var express = require('express');
var cors = require('cors');
const http = require('http');
var logger = require('./logger').logger(environment);
var serverPort = normalizePort(process.env.PORT || '8080');
//var serverPort = normalizePort(process.env.PORT || '8080');
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
//var serverPort = process.env.RTC_PORT || 31000
var serverIpAddress = process.env.RTC_IP || 'localhost'
var socketIoServer = 'etuition.azurewebsites.net' + ':' + serverPort;
////////////////////////////////////////////////
// SETUP SERVER
////////////////////////////////////////////////
var app = express();
app.set('port', serverPort);
function redirectSec(req, res, next) {
if (req.headers['x-forwarded-proto'] == 'http') {
var redirect = 'https://' + req.headers.host + req.path;
console.log('Redirect to:' + redirect);
res.redirect(redirect);
} else {
return next();
}
}
app.use(redirectSec);
require('./router')(app, socketIoServer, environment);
// Static content (css, js, .png, etc) is placed in /public
app.use(express.static(__dirname + '/public'));
app.use(cors());
// Location of our views
app.set('views', __dirname + '/views');
// Use ejs as our rendering engine
app.set('view engine', 'ejs');
// Tell Server that we are actually rendering HTML files through EJS.
app.engine('html', require('ejs').renderFile);
const server = http.createServer(app);
server.listen(serverPort);
server.on('listening', onListening);
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
logger.info("Socket IO Address:" + socketIoServer);
logger.info("Server IP Address:" + serverIpAddress);
logger.info('Server running on port ' + serverPort);
}
var io = require('socket.io').listen(server, { log: false, origins: '*:*' });
////////////////////////////////////////////////
// EVENT HANDLERS
////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
function log() {
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message: ', message);
logger.info("message: ", message);
socket.broadcast.to(socket.room).emit('message', message);
});
socket.on('create or join', function (message) {
var room = message.room;
socket.room = room;
var participantID = message.from;
configNameSpaceChannel(participantID);
io.of('/').in(room).clients(function (error, clients) {
var numClients = clients.length;
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room', room);
if (numClients == 0) {
logger.info(participantID + " joined first. Creates room " + room);
socket.join(room);
socket.emit('created', room);
} else {
logger.info(participantID + " joins room " + room);
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
}
})
});
// Setup a communication channel (namespace) to communicate with a given participant (participantID)
function configNameSpaceChannel(room) {
var nsp = '/' + room;
var socketNamespace = io.of(nsp);
logger.info('ConfigNameSpaceChannel:' + nsp);
socketNamespace.on('connection', function (socket) {
socket.on('message', function (message) {
// Send message to everyone BUT sender
socket.broadcast.emit('message', message);
});
});
return socketNamespace;
}
});

ExpressJS not call ajax get method

I created a sample ExpressJS App by using Express Application Generator
I want to call localhost:3000/data to get some data. I added these code to app.js:
app.get('/data', function(req, res){
debug("get data");
});
Also I make ajax request in client side js file :
$.ajax({
url: 'http://localhost:3000/data/',
//url: 'http://localhost:3000/data', -> also not work
type:'GET',
dataType: 'json',
success: (data) => {
console.log("get data from client js", data);
}
});
but my app.get method never call. GET /data/ 404 written in console screen. Also when I typed http://localhost:3000/data on browser I got Not Found error. how can I fix this ?
my www file :
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
I'm not exactly sure where your problem lies because there isn't enough code displayed to determine that. instead I have written a simple express server for you to use:
const express = require('express');
const bodyParser = require('body-parser');
const CORS = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(CORS());
app.get('/data', (req, res) => {
res.json({message: "working"});
});
app.listen(5000, () => {
console.log('Server listening on port 5000');
});

Socket.io not working in node.js express

I made an app with express generator, I'm using socket.io in my app but it's not working for me. I use socket.io in bin/www file and after the server created and listened to the port. after load the page that want to connect to the socket, browser's console show this error:
'socket.emmit is not a function'
here's the codes:
var app = require('../app');
var debug = require('debug')('server3:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '8585');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
};
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
};
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
};
var io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log('socket is on...');
});
It's the summary of my socket codes. the main codes are here. anyway it's not working!
The proper name of the method is socket.emit (single "m").

node.js express generator app not responding

The only file/folder ive changed is the main app.js after i installed express generator and npm install. Im a newb, maybe its its a typo in the app.js file. Heres the file and console output. Thanks so much. when i go to localhost:3000 on chrome it says cannot GET/ and it failed to load resource in console 404 and on the comm line it says GET / 404 11.960 ms - 13 everytime i reload the page
^Cryan#\Ryan:~/Desktop/node/frameworks/expressapi$ npm start
> expressapi#0.0.0 start /home/ryan/Desktop/node/frameworks/expressapi
> node ./bin/www
curl -X get http://localhost:3000/products
curl -X DELETE http://localhost:3000/products/2
and the app.js file
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var products = [
{
id: 0,
name: 'watch',
description: 'tell time with this amazing watch',
price: 30.00
},
{
id: 1,
name: 'sandals',
description: 'walk in comfort with these sansdals',
price: 10.00
},
{
id: 2,
name: 'watch',
description: 'protect your eyes!',
price: 25.00
}
];
app.get('./routes');
app.get('/products', function(req, res) {
res.json(products);
});
app.get('/products/:id', function(req, res) {
if (req.params.id > (products.length - 1) || req.params.id < 0) {
res.statusCode = 404;
res.end('NotFound');
}
res.json(products[req.params.id]);
});
app.post('/products', function(req, res) {
if (typeof req.body.name === 'undefined') {
res.statusCode = 400;
res.end('a product name is required');
}
products.push(req.body);
res.send(req.body);
});
app.put('/products/:id', function(req, res) {
if (req.params.id > (products.length - 1) || req.params.id < 0) {
res.statusCode = 404;
res.end('not found');
}
products[req.params.id] = req.body;
res.send(req.body);
});
app.delete('/products/:id', function(req, res) {
if (req.params.id > (products.length - 1) || req.params.id < 0) {
res.statusCode = 400;
res.end('not found for that id');
}
products.splice(req.params.id, 1);
res.json(products);
});
module.exports = app;
and here iis the www file
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('expressapi:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
I run your code and it looks fine. It does not have a route for "/", so you will get a Cannot GET / message in the browser if you enter http://localhost:3000/
http://localhost:3000/products returns your JSON data correctly.
For the curl command you have to use GET in capital letters:
curl -X GET http://localhost:3000/products
[{"id":0,"name":"watch","description":"tell time with this amazing watch","price":30},{"id":1,"name":"sandals","description":"walk in comfort with these sansdals","price":10},{"id":2,"name":"watch","description":"protect your eyes!","price":25}]
If you want to add the default '/' route from express generator, change your app.js file on line with this code:
var routes = require('./routes/index');
And then add the following line later on in the code:
app.use('/', routes);
You will then see the Welcome to Express message when you point your browser to http://localhost:3000
I hope that helps clarify.

node.js forward http request from 'net' server to express

I'm running the flash socket policy server on port 8484. On the same port I need to receive http requests. I'm thinking about checking whether policy-file was requested (inside the if statement below), and if it wasn't - forwarding the http request to another port where express is running (let's say localhost:3000). How can I obtain that?
// flash socket policy server
var file = '/etc/flashpolicy.xml',
host = 'localhost',
port = 8484,
poli = 'something';
var fsps = require('net').createServer(function (stream) {
stream.setEncoding('utf8');
stream.setTimeout(10000);
stream.on('connect', function () {
console.log('Got connection from ' + stream.remoteAddress + '.');
});
stream.on('data', function (data) {
console.log(data);
var test = /^<policy-file-request\/>/;
if (test.test(data)) {
console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
stream.end(poli + '\0');
} else {
console.log('Not a policy file request ' + stream.remoteAddress + '.');
stream.end('HTTP\0');
// FORWARD REQUEST TO localhost:3000 for example //
}
});
stream.on('end', function () {
stream.end();
});
stream.on('timeout', function () {
console.log('Request from ' + stream.remoteAddress + ' timed out.');
stream.end();
});
});
require('fs').readFile(file, 'utf8', function (err, poli) {
if (err) throw err;
fsps.listen(port, host);
console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});
I solved this problem a while ago, but have forgotten about the question :) The solution was to create a socket which made it possible to send and retrieve data between http express server and tcp flash policy server.
flash policy server:
var file = process.argv[2] || '/etc/flashpolicy.xml',
host = process.argv[3] || 'localhost',
port = process.argv[4] || 8484,
poli = 'flash policy data\n',
net = require('net'),
http = require('http');
var fsps = net.createServer(function (stream) {
stream.setEncoding('utf8');
stream.on('connect', function () {
console.log('Got connection from ' + stream.remoteAddress + '.');
});
stream.on('data', function (data) {
var test = /^<policy-file-request\/>/;
if (test.test(data)) {
console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
stream.end(poli + '\0');
} else {
console.log('Not a policy file request ' + stream.remoteAddress + '.');
var serviceSocket = new net.Socket();
serviceSocket.connect(3000, 'localhost', function () {
console.log('>>>> Data from 8484 to 3000 >>>>\n', data.toString());
serviceSocket.write(data);
});
serviceSocket.on("data", function (received_data) {
console.log('<<<< Data from 3000 to 8484 to client <<<<\n', received_data.toString());
stream.write(received_data);
});
}
});
stream.on('end', function () {
console.log('tcp server disconnected');
});
stream.on('timeout', function () {
console.log('Request from ' + stream.remoteAddress + ' timed out.');
});
});
require('fs').readFile(file, 'utf8', function (err, poli) {
if (err) throw err;
fsps.listen(port, host);
console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});
sample express server on localhost:3000:
var express = require('express')
, http = require('http')
, path = require('path')
, app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res){
res.send('Proper HTTP response');
});
app.post('/', function(req, res){
console.log(req.body);
res.send('Proper HTTP response');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express HTTP server listening on port ' + app.get('port'));
});

Resources