I have written app.get() for all url's which has a function. Is there a way to call a function if URL is not defined?
Angular has an otherwise method, similarly is there any other function to redirect undefined URL's in Node.js?
app.post('/registration',register);
app.get('/user',UserRegistration);
If i call a /users which is not written what function to be called to redirect it to index.html page.
If you are using Node.js without framework, your code should look like:
var http = require('http'),
url = require('url');
function parse (req, callback) {
var path = [],
u;
u = url.parse(req);
path = u.pathname.split('/');
callback(path);
}
function send_cat2 (req, res, n) {
//Generate /cat2/n
//Send page n
}
http.createServer(function(req, res) {
var path = parse(req);
if(path[0] === 'cat1') {
if(path[1] === 'page1') {
// /cat1/page1
} else if (path[1] === 'page2') {
// /cat1/page2
} else {
// 404
}
} else if (path[0] === 'cat2') {
// /cat2/n
send_cat2(req, res, parseInt(path[1]));
} else {
// 404
}
});