Saturday, 20 May 2017

Heroku MEAN Stack favicon (service not available)

I'm using node.js and trying to deploy my project into Heroku. The system run smoothly without any error on localhost. But when I deployed it, it give these errors on my console:

GET https://caretel.herokuapp.com/ 503 (Service Unavailable)
GET https://caretel.herokuapp.com/favicon.ico 503 (Service Unavailable)

and in my logs:

 /app/node_modules/serve-static/index.js:48
 var opts = Object.create(options || null)
 
 TypeError: Object prototype may only be an Object or null: public
 at Function.serveStatic [as static] (/app/node_modules/serve-static/index.js:48:21)
/* .
   .
   .
   .
*/
2017-05-20T08:02:58.519971+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=caretel.herokuapp.com request_id=de0c339a-1e05-49e0-8d3e-06860b237c44 fwd="183.171.182.30" dyno= connect= service= status=503 bytes= protocol=https

2017-05-20T08:02:59.303711+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=caretel.herokuapp.com request_id=01a6c430-e218-4b70-84e7-58d80d225ac3 fwd="183.171.182.30" dyno= connect= service= status=503 bytes= protocol=https
//...etc....

See full logs: enter image description here

And this is my starting 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');
var passport = require('passport');
require('./config/passport');
//Connect to local db
mongoose.connect('mongodb://localhost/office');

var index = require('./routes/index');
var users = require('./routes/users');
//var products = require('./routes/displays');

var app = express();

//express cors
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, sid");
  res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
  next();
});

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

//code to deploy into heroku
app.set('port', (process.env.PORT || 5000));
//code to deploy into heroku
//app.set('views', __dirname + '/views');
// 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(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')));
//code to deploy into heroku
//app.use(express.static(__dirname + '/public'));

app.use('/', index);
app.use('/users', users);
//app.use('/products', products);

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

//code to deploy into heroku
app.get('/', function(request, response) {
  response.render('pages/index');
});

//code to deploy into heroku
app.listen(app.get('port'), function() {
  console.log('Node app js running on port', app.get('port'));
});

//Tried code
/*app.listen(process.env.PORT || 3000, function(){
  console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

//Tried code
server.listen(config.port, function () {
  console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});

//Tried code
var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
    console.log('Listening on port %d', server_port);
});*/

module.exports = app;

How to solve this? Please give an example base on my codes. Thank you ^_^

*Note, I'm still new to this field. Please make your explanation simple and clearly understandable



via Azinuddin AleleCha

No comments:

Post a Comment