我把这个取自
\n\n\n\nvar AppDispatcher = require(\'../dispatcher/AppDispatcher\');\nvar EventEmitter = require(\'events\').EventEmitter;\nvar TodoConstants = require(\'../constants/TodoConstants\');\nvar assign = require(\'object-assign\');\n\nvar CHANGE_EVENT = \'change\';\n\nvar _todos = {}; // collection of todo items\n\n/**\n * Create a TODO item.\n * @param {string} text The content of the TODO\n */\nfunction create(text) {\n // Using the current timestamp in place of a real id.\n var id = Date.now();\n _todos[id] = {\n id: id,\n complete: false,\n text: text\n };\n}\n\n/**\n * Delete a TODO item.\n * @param {string} id\n */\nfunction …Run Code Online (Sandbox Code Playgroud) 在我阅读关于这个主题的另一个问题的一些答案时:
我(猜)了解export和module.exports是什么以及如何使用它们!
我不明白的是为什么我们还需要出口,因为我们只使用module.exports就可以完美地解决问题.此外,如果我认为它很好:出口有可能产生错误.
例如:
// feature.js
function Person(name) {
this.name = name;
this.greet = function() {
console.log('Hi, my name is ' + this.name);
}
}
module.exports = Person;
// app.js
var Person = require(./feature);
var Aron = new Person('Aron');
Aron.greet(); // Hi, my name is Aron
Run Code Online (Sandbox Code Playgroud)
您可以使用导出简单地中断此工作版本:
exports = Person; // wont' work
exports.Person = Person; // still won't work (until you resolve it as new Person.Person('Aron') or ..require(..).Person) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一组全局变量和函数从一个Javascript文件导出到nodejs中的另一个.
来自 node-js.include.js
var GLOBAL_VARIABLE = 10;
exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
}
};
Run Code Online (Sandbox Code Playgroud)
进入test-node-js-include.js:
var includes = require('./node-js-include');
process.stdout.write("We have imported a global variable with value " + includes.GLOBAL_VARIABLE);
process.stdout.write("\n and added a constant value to it " + includes.add(includes.GLOBAL_VARIABLE, 10));
Run Code Online (Sandbox Code Playgroud)
但变量; 我得到以下输出:
We have imported a global variable with value undefined
and added a constant value to it NaN
Run Code Online (Sandbox Code Playgroud)
为什么不出口GLOBAL_VARIABLE?
在MyModule文件夹中,我有两个JS文件.
SayHello.js
module.exports.SayHello = function() {
return('Hello !');
}
Run Code Online (Sandbox Code Playgroud)
SayByeBye.js
module.exports.SayByeBye = function() {
return('Bye Bye!');
}
Run Code Online (Sandbox Code Playgroud)
在Node.js中,我想要MyModule文件夹中的所有文件并调用函数SayHello&SayByeBye直接类似于:
require(./MyModule)
console.log(SayHello());
console.log(SayByeBye());
Run Code Online (Sandbox Code Playgroud)
编辑:
回答@Yanick Rochon,我这样做:
> ./app/my-module/index.js
global.SayHello = require('./my-module/SayHello').SayHello;
global.SayByeBye = require('./my-module/SayByeBye').SayByeBye;
Run Code Online (Sandbox Code Playgroud)
> ./app/my-module/say-hello.js
module.exports.SayHello = function() {
return('Hello !');
};
Run Code Online (Sandbox Code Playgroud)
> ./app/my-module/say-byebye.js
module.exports.SayByeBye = function() {
return('Bye Bye !');
};
Run Code Online (Sandbox Code Playgroud)
> ./app/main.js
require('./my-module');
console.log(SayHello());
console.log(SayByeBye());
Run Code Online (Sandbox Code Playgroud)
节点文档中有关于全局对象的部分.
但是,应谨慎使用全局变量.通过向全局空间添加模块,我降低了可测试性和封装.但在这种情况下,我认为使用这种方法是可以接受的.
我在 ubuntu 16.04 上使用 nodejs (6.2.1) 和 mongoose(4.4.16) 和 mongodb(2.1.21) 并创建一个注册表单,但是当我提交 mongoose 没有创建文件并请求继续等待。请帮我解决这个问题。下面是文件结构
视图位于 app-server 文件夹上,非常完美,所以我不包括该结构
app-api
??? controllers
? ??? users.js
??? models
? ??? db.js
? ??? users.js
??? routes
??? index.js
Run Code Online (Sandbox Code Playgroud)
var express = require('express');
var router = express.Router();
var ctrlUsers = require('../controllers/users');
router.get('/user', ctrlUsers.userInfo);
router.post('/signup', ctrlUsers.userSignup);
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
var mongoose = require( 'mongoose' );
var mongoURI = 'mongodb://localhost/local';
var mongoDB = mongoose.createConnection(mongoURI);
mongoDB.on('connected', function (){
// console.log("enviorment:" + process.env.NODE_ENV);
// console.log("mongolab:" …Run Code Online (Sandbox Code Playgroud) 在向我的快速处理程序添加路由后,出现错误(见下文):
/app/node_modules/express/lib/router/index.js:458
2017-01-03T15:53:48.842543+00:00 app[web.1]: throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
2017-01-03T15:53:48.842545+00:00 app[web.1]: ^
2017-01-03T15:53:48.842545+00:00 app[web.1]:
2017-01-03T15:53:48.842546+00:00 app[web.1]: TypeError: Router.use() requires middleware function but got a Object
2017-01-03T15:53:48.842547+00:00 app[web.1]: at Function.use (/app/node_modules/express/lib/router/index.js:458:13)
2017-01-03T15:53:48.842548+00:00 app[web.1]: at EventEmitter.<anonymous> (/app/node_modules/express/lib/application.js:219:21)
2017-01-03T15:53:48.842549+00:00 app[web.1]: at Array.forEach (native)
2017-01-03T15:53:48.842549+00:00 app[web.1]: at EventEmitter.use (/app/node_modules/express/lib/application.js:216:7)
2017-01-03T15:53:48.842550+00:00 app[web.1]: at module.exports (/app/server/routes/index.js:16:9)
2017-01-03T15:53:48.842551+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:59:27)
2017-01-03T15:53:48.842551+00:00 app[web.1]: at Module._compile (module.js:556:32)
2017-01-03T15:53:48.842552+00:00 app[web.1]: at Object.Module._extensions..js (module.js:565:10)
2017-01-03T15:53:48.842553+00:00 app[web.1]: at Module.load (module.js:473:32)
2017-01-03T15:53:48.842553+00:00 app[web.1]: at …Run Code Online (Sandbox Code Playgroud)