假设我有一个叫app。js的文件。很简单:
var express = require('express');
var app = express.createServer();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.render('index', {locals: {
title: 'NowJS + Express Example'
}});
});
app.listen(8080);
如果我在“tools.js”中有一个函数。我如何将它们导入到apps.js中使用?
还是……我应该把“工具”变成一个模块,然后需要它吗?<<似乎很难,我宁愿做tools.js文件的基本导入。
下面是一个简单明了的解释:
Server.js内容:
// Include the public functions from 'helpers.js'
var helpers = require('./helpers');
// Let's assume this is the data which comes from the database or somewhere else
var databaseName = 'Walter';
var databaseSurname = 'Heisenberg';
// Use the function from 'helpers.js' in the main file, which is server.js
var fullname = helpers.concatenateNames(databaseName, databaseSurname);
Helpers.js内容:
// 'module.exports' is a node.JS specific feature, it does not work with regular JavaScript
module.exports =
{
// This is the function which will be called in the main file, which is server.js
// The parameters 'name' and 'surname' will be provided inside the function
// when the function is called in the main file.
// Example: concatenameNames('John,'Doe');
concatenateNames: function (name, surname)
{
var wholeName = name + " " + surname;
return wholeName;
},
sampleFunctionTwo: function ()
{
}
};
// Private variables and functions which will not be accessible outside this file
var privateFunction = function ()
{
};
您不需要新的函数或新的模块。
如果不想使用命名空间,只需执行正在调用的模块。
在tools.js
module.exports = function() {
this.sum = function(a,b) { return a+b };
this.multiply = function(a,b) { return a*b };
//etc
}
在app.js
或任何其他。js,如myController.js:
而不是
Var tools = require('tools.js'),这迫使我们使用命名空间并调用工具,如tools.sum(1,2);
我们可以简单地调用
require('tools.js')();
然后
sum(1,2);
在我的情况下,我有一个文件与控制器ctrl .js
module.exports = function() {
this.Categories = require('categories.js');
}
和我可以使用类别在每个上下文后的公共类require('ctrls.js')()
包含文件并在给定的(非全局)上下文中运行它
fileToInclude.js
define({
"data": "XYZ"
});
main.js
var fs = require("fs");
var vm = require("vm");
function include(path, context) {
var code = fs.readFileSync(path, 'utf-8');
vm.runInContext(code, vm.createContext(context));
}
// Include file
var customContext = {
"define": function (data) {
console.log(data);
}
};
include('./fileToInclude.js', customContext);
要把“工具”变成一个模块,我一点也不觉得困难。尽管有其他的答案,我仍然建议使用module.exports:
//util.js
module.exports = {
myFunction: function () {
// your logic in here
let message = "I am message from myFunction";
return message;
}
}
现在我们需要将这个exports分配到全局作用域(在你的应用程序|index|server.js中)
var util = require('./util');
现在你可以引用和调用函数为:
//util.myFunction();
console.log(util.myFunction()); // prints in console :I am message from myFunction
创建两个JavaScript文件。例如import_functions.js和main.js
1.) import_functions.js
// Declaration --------------------------------------
module.exports =
{
add,
subtract
// ...
}
// Implementation ----------------------------------
function add(x, y)
{
return x + y;
}
function subtract(x, y)
{
return x - y;
}
// ...
2.) main.js
// include ---------------------------------------
const sf= require("./import_functions.js")
// use -------------------------------------------
var x = sf.add(4,2);
console.log(x);
var y = sf.subtract(4,2);
console.log(y);
输出
6
2