0

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!

4

1 回答 1

1

我发现了问题所在,希望对某人有所帮助:

我的项目结构有两个文件夹:一个名为 Client,我所有的 html 和 .js 都来自 angular where。另一个文件夹是 WebServer,我有所有的 nodejs 文件。

为了部署到 nodejitsu,您运行一个命令,即jitsu deploy,这又运行另一个命令:npm pack. 此命令创建一个 .tgz 文件,其中包含 nodejs 目录中的所有数据,不包括 node_modules 文件和任何以.. 问题是,如果您像我一样在该文件夹之外有文件,则不会包含它们。

解决方案是将您的客户端文件夹移动到 nodejs 中。您需要发送到服务器的所有内容都应该在此文件夹中。

于 2014-08-27T01:21:08.897 回答