I am new to JS and trying to break the code into multiple modules. I am running nodejs and I am puzzled here on why is it complaining about pathChecker not defined. Any ideas on it?
<
const http = require('http');
const parseUrl = require('parseurl');
const path = require('path');
http.createServer( function (req, res)
{
try
{
// this is library function
var pathName = decodeURIComponent(parseUrl(req));
// create a literal validateFile to validate the path
var validateFile = new pathChecker(pathName);
// This is an engine to validate the path problems related to security, existence etc.
validateFile.pathCheck();
if(validateFile.error === true) {
res.statusCode = validateFile.statusCode;
res.end(validateFile.ErrorMsg);
return;
}
}
catch(err)
{
res.statusCode = err.status || 500;
res.end(err.message);
}
}).listen(4000);
I have another file called
errorHandler.js
function pathChecker(path)
{
this.error = true;
this.path = path;
this.statusCode = 500;
this.ErrorMsg = "Internal Server Error";
this.pathCheck = function()
{
if(!path)
{
this.statusCode = 400;
this.ErrorMsg = 'path required';
this.error = true;
}
else{
this.statusCode = 200;
this.ErrorMsg = undefined;
this.error = false;
}
}
};
On running this, I get the output
pathChecker is not defined