0

I am trying to deploy my node.js app to my web server. I have everything installed on the server side, and the app works locally. I just seem unable to connect when I try to upload it to the server. It tries to load and eventually gives me a 504 time out error.

Here's my code:

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 session = require('express-session');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var multer  =   require('multer');
var app =   express();
var fs = require('fs');
var mongoose = require('mongoose');
var unqID;
var http = require('http');


//http
http.createServer(function(req, res){
  console.log('SmartMove Running');

mongoose.connect('mongodb://localhost/smartproperty');
var db = mongoose.connection;
var routes = require('./routes/index');
var articles = require('./routes/articles');
var categories = require('./routes/categories');
var manage = require('./routes/manage');
var manager = require('./routes/manager');
var adverts = require('./routes/adverts');
var upload = require('./routes/server');

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')));
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true
}))

app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
      var namespace = param.split('.')
      , root    = namespace.shift()
      , formParam = root;

    while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param : formParam,
      msg   : msg,
      value : value
    };
  }
}));

app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

app.use('/', routes);
app.use('/articles', articles);
app.use('/categories', categories);
app.use('/manage', manage);
app.use('/manager', manager);
app.use('/adverts', adverts);

//res.end('Done');
}).listen(8080, '127.0.0.1');
//end http

//upload
app.use(bodyParser.json());
var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  //filename: function (req, file, callback) {
    //callback(null, file.fieldname + '-' + Date.now());
  //}

  filename: function (req, file, callback) {
    var advert = new Advert();
    var qryId = advert._id;
    callback(null, unqID + '-' + Date.now());
  }
});

var upload = multer({ storage : storage }).array('userPhoto',2);

//app.get('/',function(req,res){
  //    res.sendFile(__dirname + "/index.html");
//});

app.post('/api/photo',function(req,res){
    upload(req,res,function(err) {
        console.log(req.body);
        console.log(req.files);
        if(err) {
            return res.end("Error uploading file.");
        }
        res.end("File is uploaded");
    });
});
//end upload

// 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: {}
  });
});

module.exports = app;

I have tried to see if it will load with just a res.end. This is successful, however, with that line removed nothing loads at all.

If anyone could help that would be great.

EDIT

Ok so i have managed to get it to depoy on my server but now this is presenting a different problem. all of the files have uploaded and i can see them, when the server is running though it fails to go to any routes, and it also seems like my CSS hasnt pulled through like in my development environment. Does anyone have any ideas why this may be, it still seems to be an issue on the server side which is why i edited, however if I am wrong to do this please let me know and i will create a new question.

Mikecul
  • 63
  • 8

0 Answers0