0

Currently this is what I have as my nodejs/expressjs serverside code:

// Init Express Web Framework
var express = require('express');
var app = express();
var path = require('path');


// Set view engine to EJS & set views directory
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', path.resolve(__dirname, 'client', 'views'));

app.use(express.static(path.resolve(__dirname, 'client')));

//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');
//    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
//    if (req.method === 'OPTIONS') {
//        res.send(200);
//    }
//    else {
//        next();
//    }
//});


// Database Connection
var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');
require('./server/routes/capture');
require('./server/routes/comment');
require('./server/routes/vote');
require('./server/routes/follow');
mongoose.connect(configDB.url);

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));               
app.use(bodyParser.text());                                    
app.use(bodyParser.json({ type: 'application/json'}));

// Main route
app.get('/', function(req, res){
    res.render('index.html');
});

// API 
var api = express.Router();
require('./server/routes/capture')(api);
require('./server/routes/comment')(api);
require('./server/routes/vote')(api);
require('./server/routes/follow')(api);
app.use('/api', api);

app.use(function(err, req, res, next) {
    res.status(err.status || 50, function(res) {
        res.render('error.html');
    });
});

// Port Settings
app.listen(process.env.PORT || 3000, process.env.IP);
console.log('Listening on port ' + process.env.PORT);

In my client I have 2 views: my index.html where all my content renders, and I have a error.html where I want to redirect people to if they get a 500 error. Currently if people go to a link that does not exist, they get redirected to the main page.

But when I use an url that exists but with a parameter that doesnt, I get the 500 (Internal server error).

Using the code on the bottom of my serverside, I would think that these errors get redirected to the error.html page, but for some reason it doesn't.

On my server log it gives no errors, seeing it should be redirecting, but on my client side, I simply load the page with the wrong parameter (and ofcourse no content, except for the empty elements, seeing nothing is found).

How do I solve this?

Thanks

Pex
  • 519
  • 2
  • 12
  • 30
  • Can you give an example URL of "when I use an url that exists but with a parameter that doesn't, I get the 500 (Internal server error)."? – ccnokes Jun 03 '16 at 03:33
  • `URL/#/detail/{{CorrectParameter}}` vs `URL/#/detail/{{WrongParameter}}` or `URL/#/detail/NOTHING` and with the last one, even if I leave the parameter open, it goes to the detailpage. – Pex Jun 03 '16 at 09:11
  • If the offending URLs have the `#` in it, this is an angular problem because everything after that hashtag is handled by angular. TBH, you may want to get rid of the error page in express and just serve the angular app's index.html for all non-API requests. I've found that to be easiest. This answer may help: http://stackoverflow.com/questions/28459975/mean-stack-angular-routing-vs-express-routing – ccnokes Jun 03 '16 at 17:53

1 Answers1

0

One way is by simply define error route, it would be looks like:

// Error route
app.get('/error', function(req, res){
    res.render('error.html');
});

then, redirect to /error route if (any of) error occurs i.e., from your server routes (*handle error in each of your server routes).

like:

res.redirect('/error');

Hope this helps.

narainsagar
  • 1,079
  • 2
  • 13
  • 29