i am unable to start my server.js file on Ubuntu server.I installed all npm packages successfully.But when i run command node server.js The script is not executing at all and there is no error also. After hit enter it is again move back to the folder path in putty ssh.I am explaining my code below.
server.js:
var port=8888;
var express=require('express');
var morgan = require('morgan');
var http=require('http');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var mongo = require('mongojs');
var database='Oditek';
var collections=['video'];
var app= express();
var server=http.Server(app);
var io=require('socket.io')(server);
var db = mongo.connect("127.0.0.1:27017/"+database, collections);
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
db.on('ready', function () {
console.log('database connected')
});
app.get('/',function(req,res){
res.sendfile('view/login.html');
});
app.post('/login',function(req,res){
var username=req.body.username;
var password=req.body.userpassword;
if(username && password){
db.video.findOne({
username:username,
password:password
},function(err,doc){
if(doc){
console.log('login',doc);
res.send(doc);
}
if(err){
console.log('login12',err);
res.send("could not login");
}
});
}
});
app.get('/index',function(req,res){
res.sendfile('view/index.html');
});
app.get('/video',function(req,res){
res.sendfile('view/video.html');
});
app.get('/whiteboard',function(req,res){
res.sendfile('view/whiteboard.html');
});
//socket----programming//
var roomid;
var clients={};
io.on('connection',function(socket){
//console.log('socket id',socket);
if(socket.handshake.query.roomid){
roomid=socket.handshake.query.roomid;
}
var usertype=socket.handshake.query.usertype;
//var url=socket.handshake.headers.referer;
//var myString = url.substr(url.indexOf("?") + 1);
//var usertype=myString.substr(myString.indexOf("=")+1);
//console.log('usertype',usertype);
clients[usertype]={
"socket":socket.id
}
console.log('clients',clients['admin'].socket);
socket.on('admin-join',function(data){
if(data.IsJoinAdmin){
socket.join(roomid);
}
});
socket.on('user-join',function(data){
console.log('user wants to join',data);
//console.log('user type',clients);
if(data.isUserJoin){
io.sockets.connected[clients[data.usertype].socket].emit('user-already-joined',data);
socket.join(roomid);
}
});
socket.on('send-playing-video-request-to-users',function(data){
io.to(roomid).emit('sending-broadcasting',data);
});
socket.on('send-broadcasting-message',function(data){
io.to(roomid).emit('sending-broadcasting',data);
});
socket.on('share-white-board',function(msg){
io.to(roomid).emit('sharing-white-board',msg);
});
socket.on('disconnect', function() {
for(var user in clients) {
if(clients[user].socket === socket.id) {
delete clients[user];
io.to(roomid).emit('user-has-left',{userleft:true});
break;
}
}
})
});
server.listen(port);
console.log('server is listening on the port'+port);
at least the console message should execute if this file is running.But i am not getting like that.Please help me to resolve this error.
There is another program called node which would show no output when called named "Amateur Packet Ratio Node program". From chatting with you, I have determined that this is what is on your system. There are two basic solutions.
Remove "Armature Packet Ratio Node program", and reinstall node.js
.
sudo apt-get purge node nodejs # Uninstall both
sudo apt-get install nodejs # Reinstall nodejs
Living with both programs and accessing node.js through node.js
.
nodejs server.js
forever -c 'nodejs server.js' start
Related
I'm trying to parse and insert a big csv file into MongoDB but when the file extends 100'000 rows I get a bad response from the server. And the files I need to insert are usually above 200'000 rows.
I've tried both bulk insert (insertMany) and Babyparse(Papaparse) streaming approach to insert the file row by row. But with poor results.
Node api:
router.post('/csv-upload/:id', multipartMiddleware, function(req, res) {
// Post vartiables
var fileId = req.params.id;
var csv = req.files.files.path;
// create a queue object with concurrency 5
var q = async.queue(function(row, callback) {
var entry = new Entry(row);
entry.save();
callback();
}, 5);
baby.parseFiles(csv, {
header: true, // Includes header in JSON
skipEmptyLines: true,
fastMode: true,
step: function(results, parser) {
results.data[0].id = fileId;
q.push(results.data[0], function (err) {
if (err) {throw err};
});
},
complete: function(results, file) {
console.log("Parsing complete:", results, file);
q.drain = function() {
console.log('All items have been processed');
res.send("Completed!");
};
}
});
});
This streaming approach results in: POST SERVER net::ERR_EMPTY_RESPONSE
Not sure if I'm using the async.queue correctly though.
Is there a better and more efficient way to do this OR am I doing something wrong?
Express Server:
// Dependencies
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var routes = require('./server/routes');
var mongoose = require("mongoose");
var babel = require("babel-core/register");
var compression = require('compression');
var PORT = process.env.PORT || 3000;
// Include the cluster module
var cluster = require('cluster');
mongoose.connect(process.env.MONGOLAB_URI || 'mongodb://localhost/routes');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Code to run if we're in a worker process
} else {
// Express
var app = express();
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
// Compress responses
app.use(compression());
// Used for production build
app.use(express.static(path.join(__dirname, 'public')));
routes(app);
// Routes
app.use('/api', require('./server/routes/api'));
app.all('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Start server
app.listen(PORT, function() {
console.log('Server ' + cluster.worker.id + ' running on ' + PORT);
});
}
Handling the import:
Great question, from my experience by far the fastest way to insert a csv into mongo is via the command line:
mongoimport -d db_name -c collection_name --type csv --file file.csv --headerline
I don't believe mongoose has a way of calling mongoimport (someone correct me if I'm wrong)
But it's simple enough to call via node directly:
var exec = require('child_process').exec;
var cmd = 'mongoimport -d db_name -c collection_name --type csv --file file.csv --headerline';
exec(cmd, function(error, stdout, stderr) {
// do whatever you need during the callback
});
The above will have to be modified to be dynamic, but it should be self-explanatory.
Handling the upload:
Uploading the file from a front-end client is another challenge.
Most browsers will timeout if you make a request to a server and don't get a response within 60 seconds (probably what you are referring to above)
One solution would be to open a socket connection (search for socket.io in npm) for details. This will create a constant connection to the server and won't be subject to the timeout restrictions.
If uploading is not an issue, and the timeout is due to the slow parsing/inserting then you may not have to worry about this once you implement the above.
Other considerations:
I'm not sure exactly what you need to send back to the user, or what parsing needs to take place. But that can either be done outside of the normal request/response cycle, or can be handled during a socket connection if it's needed during one request/response cycle.
I'm new to using mongo and having difficulties with set-up on local OSX server.
When I run node app.js my page appears on http://127.0.0.1:3000/ but I get this error:
Firefox can't establish a connection to the server at ws://127.0.0.1:3000/socket.io/?EIO=3&transport=websocket&sid=************. socket.io.js:5325:0
The connection to ws://127.0.0.1:3000/socket.io/?EIO=3&transport=websocket&sid=*************** was interrupted while the page was loading.
and when I refresh the page in firefox I get Unable to connect server at 127.0.0.1:3000. and the following error in terminal:
a user connected
/Users/******/express_intro/node_modules/mongodb/lib/url_parser.js:15
if(url.indexOf("mongodb://") != 0)
^
TypeError: Cannot read property 'indexOf' of undefined
at module.exports (/Users/******/express_intro/node_modules/mongodb /lib/url_parser.js:15:9)
at Function.MongoClient.connect (/Users/******/express_intro/node_modules/mongodb/lib/mongo_client.js:95:16)
at Namespace.<anonymous> (/Users/******/express_intro/app.js:48:8)
at Namespace.emit (events.js:107:17)
at Namespace.emit (/Users/******/express_intro/node_modules/socket.io/lib/namespace.js:205:10)
at /Users/******/express_intro/node_modules/socket.io/lib/namespace.js:172:14
at process._tickCallback (node.js:355:11)
Here is the app.js
var express= require('express');
var bodyParser=require('body-parser');
var path=require('path');
var app=express();
var mongo=require('mongodb').MongoClient;
var http = require('http'); //the variable doesn't necessarily have to be named http
//configure app
app.set('port',process.env.PORT || 3000);
app.set('view engine','jade');
app.set('views',path.join(__dirname,'views'));
//use middleware
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.urlencoded({ extended: true }));
//define routes
var todoItems=[
{id:1,desc:'foo'},
{id:2,desc:'bar'},
{id:3,desc:'baz'}
];
app.get('/',function(req,res){
res.render('index',{
title:'My App',
items:todoItems
});
});
var serve=http.createServer(app);
var io=require('socket.io')(serve);
serve.listen(app.get('port'), function(){
console.log('Ready on port ' + app.get('port'));
});
io.on('connection',function(socket){
console.log('a user connected');
mongo.connect(process.env.CUSTOMCONNSTR_MONGOLAB_URI,function(err,db){
if(err){
console.warn(err.message);
}
else{
var collection=db.collection('chat messages')
var stream=collection.find().sort().limit(10).stream();
stream.on('data',function(chat){
console.log('emitting chat');
socket.emit('chat',chat.content);
});
}
});
});
Here is the client code
//var socket=io();
var socket = io('http://localhost:3000/');
$('#send-message-btn').click(function(){
var msg=$('#message-box').val();
socket.emit('chat',msg);
$('#messages').append($('<p>').text(msg));
$('#message-box').val('');
return false;
});
socket.on('chat',function(msg){
$('#message').append($('<p>').text(msg));
});
I also get this on loading the page first time
Any help appreciated.
Thanks
I'm working on my first node project, I basically followed the thinkster post to get me started. I've managed to build a simple app and now I'm trying to configure socket.io.
The socket.io initialization code and event handlers are not hard to understand, what is really confusing me is how should I organize that code between the bin/www and the app.js files. Both files were generated automatically by express. bin/www depends on the app.js module, the first initiates the server variable which is needed to start up the socket module, so that means I should put all the 'socket.io' code in the bin/www file?
I don't think I should be touching that file though, I would be more comfortable putting that code into app.js or even inside a dedicated file. I think I need to pass the server object reference between modules, but I'm not sure how to do that.
This is the content of the bin/www file:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('oculus: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);
}
And this the content of 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 mongoose = require('mongoose');
// Mongoose
require('./models/Aplicacoes');
mongoose.connect('mongodb://localhost/oculus');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__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')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/**
* Some business code here
*/
....
module.exports = app;
First, let me tell you, what you're trying to do is kinda right. Not to mess with the bin/www would be fine.
But remember express generator is just that. A generator for you to build upon. You generate, and the apply your own modifications.
My choice would be to:
copy bin/www to a new bin/wwwio,
Update the bin/wwwio script to attach socket.io to the created http server.
Update bin/wwwio to require() a new file ../io.js that handles all my socket.io events.
Modifiy package.json to run node ./bin/wwwio on npm start instead of bin/www
You can also look at the answers on this other question about the some topic:
Using socket.io in Express 4 and express-generator's /bin/www
You'll find several approaches to achieving modularity with little touching on the bin/www script.
Try following these simple steps
Install Socket.io with the following command:
npm install --save socket.io
Add the following to app.js:
var sockIO = require('socket.io')(); app.sockIO = sockIO;
In bin/www,
after var server = http.createServer(app), add the following:
var sockIO = app.sockIO; sockIO.listen(server);
To test functionality, in app.js, you can add the line:
sockIO.on('connection', function(socket){ console.log('A client connection occurred!'); });
Now in layout.hbs add the following snippet before the body closing tag < /body >:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io(); </script>
Further, I have created the GIT REPOSITORY for the complete working project of chat with Sockets express generator.
https://github.com/Mohsin05/Sockets-Express-Generator
I hope it will help everyone. ;)
I've been bouncing back and forth between socket.io and express.io - but settled for socket.io with Express 4, as I would like to use Namespaces.
I have worked on some examples of having an Express 4 Server using Socket.io - but most examples are based on one file with everything in it. I am trying to separate all my code to make it easier but I am at a loss as to how to add Socket.io (or where).
I have index.js which uses Cluster and basically calls server.js:
var server = require( "./server.js" );
var cluster = require('cluster');
var webApp={
run: function(){
console.log('Starting: Server');
server.listen();
}
};
if(cluster.isMaster){
cluster.fork();
cluster.on('exit',function(worker){
console.log('Worker ' + worker.id + ' died..');
setTimeout( function () { cluster.fork(); }, 1000 );
});
} else{
try {
webApp.run();
}
catch(e)
{
console.log(e);
process.exit(1);
}
process.on('uncaughtException', function(err){
console.log(err);
process.exit(1);
});
process.on( 'SIGINT', function () {
console.log( "\n SIGINT (Crtl-C)" );
//Kill worker
cluster.disconnect();
process.exit(1);
});
}
This then calls the server.js file:
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var config = require('./config/config.js');
var router = require('./routes');
var Server = Object.subClass({
/**
* Constructor
*/
init:function(){
this.appServer = express();
var that = this;
var appServer = this.appServer;
appServer.use(express.static(__dirname + '/public'));
appServer.set('views', path.join(__dirname, 'views'));
appServer.set('view engine', 'ejs');
appServer.use(bodyParser.urlencoded({ extended: true }));
appServer.use(bodyParser.json());
appServer.get('/',router.root);
},
/**
* Listener HTTP
*/
listen:function(){
var port = config.rest.port;
console.log(':: on port:' + port);
this.appServer.listen(port);
}
});
module.exports = new Server();
I am only having one 'route', which is the '/' and is defined in routes.js file. The page loads fine but where do I add the server side socket.io? and do I add any socket.io namespace definitions in the routes.js file or in the javascript of the page being loaded?
There are so many ways of using sockets that I can't seem to work out the best approach for my multi-file approach.
Any help would be brilliant as I seem to be going in circles.
Enjoy our Saturday :)
Thanks again.
I've spent the morning looking at the Cluster/Worker approach and decided to use 'SocketCluster' as it seems to do what I need.
Enjoy your Sunday
I'm using Greg Franko's great Backbone-Require-Boilerplate and I was thinking about SEO so I found Phantom.js
Now I'm trying to integrate it and found.
http://backbonetutorials.com/seo-for-single-page-apps/
which looks like the answer but just couldn't make this work. I have PhantomJs installed.
and my server.js is
// DEPENDENCIES
// ============
var express = require("express"),
http = require("http"),
port = (process.env.PORT || 8001),
server = module.exports = express();
// SERVER CONFIGURATION
// ====================
server.configure(function() {
server.use(express["static"](__dirname + "/../public"));
server.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
server.use(server.router);
server.get(/(.*)/, respond);
});
// SERVER
// ======
// Start Node.js Server
var app = http.createServer(server);
app.listen(port);
So how would I ever plug Phantom.js in this?
if you are looking for a node integration with express, please look at this:
https://github.com/Obvious/phantomjs
EDIT:
and here is a working phantomjs node-module:
https://github.com/amir20/phantomjs-node
var phantom = require('phantom');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.open('https://stackoverflow.com/').then(function(status) {
console.log(status);
page.property('content').then(function(content) {
console.log(content);
page.close();
ph.exit();
});
});
});
});