we're currently trying to build a nodejs app that works with express-generator. I'm currently trying to add the Primus websocket into my application. The problem is that if I don't add app.listen(port) into my app.js file, my app won't show on localhost:3000, even though it's included in the bin/www-file. If I add app.listen(port), my app can't seem to find the primus library although I installed it into my dependencies. I don't know if the problem that my app can't find the primus library is related to the fact that I whether or not add app.listen(port).
My app.js file [
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var Strategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/facebookusers');
var http = require("http");
var Primus = require("primus");
var options = {
transformer: "engine.io"
}
var facebook = require('./routes/facebook');
var users = require('./routes/users');
var profile = require('./routes/profile');
var login = require('./routes/login');
var admin = require('./routes/admin');
var vote_now = require('./routes/vote_now');
var vote_result = require('./routes/vote_result');
var groups = require('./routes/groups');
var endvoting = require('./routes/endvoting');
var roles = require('./routes/roles');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// 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')));
app.use(require('morgan')('combined'));
app.use(session({ secret: 'marioWim' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use('/users', users);
app.use('/profile/', profile);
app.use('/login', login);
app.use('/admin/', admin);
app.use('/vote_now', vote_now);
app.use('/vote_result', vote_result);
app.use('/auth/facebook', facebook);
app.use('/groups', groups);
app.use('/endvoting', endvoting);
app.use('/roles',roles);
// 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 handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(3000);
module.exports = app;
my package.json file
{
"name": "demo-or-die",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "^1.17.1",
"connect-ensure-login": "^0.1.1",
"connect-flash": "^0.1.1",
"cookie-parser": "^1.4.3",
"debug": "~2.6.3",
"engine.io": "^3.1.0",
"express": "~4.15.2",
"express-session": "^1.15.2",
"flash": "^1.1.0",
"jade": "~1.11.0",
"mongoose": "^4.9.6",
"morgan": "^1.8.1",
"named-routes": "^2.0.6",
"passport": "^0.3.2",
"passport-facebook": "^2.1.1",
"primus": "^7.0.1",
"prompt": "^1.0.0",
"pug": "^2.0.0-beta.12",
"serve-favicon": "~2.4.2"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-changed": "^3.0.0",
"gulp-imagemin": "^3.2.0",
"gulp-nodemon": "^2.2.1",
"gulp-sass": "^3.1.0"
}
}
my bin/www
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('demo-or-die: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);
}
via Mario Samison
No comments:
Post a Comment