Run NodeJS app on nodejitsu, all i get is "cannot get /"

297 views Asked by At

as part of my learning i wanted to deploy my app to nodejitsu. Its running fine on my local server, but on nodejitsu all i get is

Cannot GET /

I thought it may have something to do with the NODE_ENV set to production on the server, but i never touched this on my local server. I changed it on nodejitsu to development but still i cant get it to work.

After commenting all the code i think the problem is in my index.js which i show below:

var express = require('express');//the framework
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//var session = require('express-session');
var methodOverride = require('method-override');
var passport = require("passport");//for authentication
var LocalStrategy = require('passport-local').Strategy;//local users for passport
var http = require('http');
var path = require('path');
var db = require('./dataBase');//to connect to the db
var userRoles = require('./routingConfig').userRoles;
var accessLevels = require('./routingConfig').accessLevels;

var debug = false;

var db = new db('inmobiliaria', 'localhost', 3306, debug);

require('./passport')(passport, db, debug)
var app = express();

app.set('port', 1337);
app.use(methodOverride());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }))
//app.use(session({ secret: 'SECRET' }));
app.use(passport.initialize());
app.use(passport.session());

app.use(function (req, res, next) {
    var role = userRoles.public;//default role
    var username = '';

    if (req.user) {
        role = req.user.role;
        username = req.user.username;
    }
    res.cookie('user', JSON.stringify({
        'username': username,
        'role': role
    }));

    next();
});

app.use('/', express.static(path.join(__dirname + '/../Client/')));
app.use("/lib", express.static(path.join(__dirname + '/../Client/lib/')));
app.use("/ctr", express.static(path.join(__dirname + '/../Client/Controllers/')));
app.use("/media", express.static(path.join(__dirname + '/../Client/media/')));
app.use("/views", express.static(path.join(__dirname + '/../Client/Views/')));
app.use("/srv", express.static(path.join(__dirname + '/../Client/Services/')));
app.use("/dct", express.static(path.join(__dirname + '/../Client/Directives/')));

require('./routes')(app, passport, db);
//require('./emailConfiguration');

http.createServer(app).listen(process.env.PORT || app.get('port'), function (err) {
    console.log("Express server listening on port " + app.get('port'));
    if (err) {
        throw err; // For completeness's sake.
    }
});

I investigated about this variable, but im not sure it has something to do with it. This is the url http://horaciotest.jit.su/, in case you want to see it. Is this configuration? Am i doing something that should not be done?

Thanks for taking your time.

EDIT:

i managed to reduce the error case to a few lines i think. As the guys at nodejitsu suggested, im now trying to use the module node-static to serve static files, but i cant get it to work along express:

this code works on nodejitsu and my local server (or at least doesnt show any errors)

var statik = require('node-static');
var http = require('http');
var path = require('path');

var file = new (statik.Server)(path.join(__dirname + '/../Client/'));//index.html here
http.createServer(function (req, res) {
    console.log(path.join(__dirname + '/../Client/'));
    file.serve(req, res);
}).listen(8080);

but as soon as i add express, i get the error i mentioned above:

var express = require('express');    
var statik = require('node-static');
var http = require('http');
var path = require('path');

var app = express();

var file = new (statik.Server)(path.join(__dirname + '/../Client/'));//index.html here
http.createServer(app, function (req, res) {
    console.log(path.join(__dirname + '/../Client/'));
    file.serve(req, res);
}).listen(8080);

Can someone tell me why when i add the express app i get the error? it may be what i need to get this to work on nodejitsu, thanks!

1

There are 1 answers

0
Toddy On BEST ANSWER

I found out what the problem was, hope it helps someone:

My project structure had two folders: one was named Client, where all my html and .js from angular where. The other folder was WebServer, where i had all my nodejs files.

In order to deploy to nodejitsu, you run a command which is jitsu deploy, this in turn runs another command: npm pack. This command creates a .tgz file with all the data in you nodejs directory excluding the node_modules file and any file that starts with .. Problem is, if you like me have files outside that folder, they wont be included.

The solutions is to move your client folder inside the nodejs one. Everything you need to sent to the server should be in side this folder.