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