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?
我是JS的新手,尝试将代码分解成多个模块。我正在运行nodejs,我在这里感到困惑的是为什么它要抱怨没有定义的pathChecker。有什么想法吗?
<
<
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
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
pathChecker没有定义
2 个解决方案
#1
2
You need to export and import the file as a module. You do this like this:
您需要将文件作为模块导出和导入。你这样做:
// File A.js
function A() {
}
module.exports = A;
// File B.js
var A = require("./A");
A();
Note that the name A is arbitrary on the import and you can name whatever you want. You can also export an object with functions on it instead of a single function and then when importing you can get properties off of it. This way you can export more than one function or value from a single file.
请注意,名称A在导入上是任意的,您可以命名任何您想要的名称。您还可以导出带有函数的对象,而不是单个函数,然后在导入时,您可以从其中获取属性。这样,您可以从一个文件中导出多个函数或值。
#2
0
You need to export the function in your errorHandler.js file.
您需要在errorHandler中导出函数。js文件。
function pathChecker(path) {
...
}
module.exports = pathChecker;
then import it into your main file with
然后将它导入到主文件中
const pathChecker = require("./errorHandler")
#1
2
You need to export and import the file as a module. You do this like this:
您需要将文件作为模块导出和导入。你这样做:
// File A.js
function A() {
}
module.exports = A;
// File B.js
var A = require("./A");
A();
Note that the name A is arbitrary on the import and you can name whatever you want. You can also export an object with functions on it instead of a single function and then when importing you can get properties off of it. This way you can export more than one function or value from a single file.
请注意,名称A在导入上是任意的,您可以命名任何您想要的名称。您还可以导出带有函数的对象,而不是单个函数,然后在导入时,您可以从其中获取属性。这样,您可以从一个文件中导出多个函数或值。
#2
0
You need to export the function in your errorHandler.js file.
您需要在errorHandler中导出函数。js文件。
function pathChecker(path) {
...
}
module.exports = pathChecker;
then import it into your main file with
然后将它导入到主文件中
const pathChecker = require("./errorHandler")