相关疑难解决方法(0)

如何创建NodeJS模块?

我已经阅读了NodeJS网站的详细信息:https://nodejs.org/api/modules.html.我不明白模块是如何工作的,创建模块的最小步骤是什么,以及npm如何帮助我.

我该如何创建模块?
我如何使用模块?
把它放在npm上意味着什么?

注意:这是一个自我回答的问题,目的是将知识作为规范分享.

node.js

6
推荐指数
2
解决办法
1964
查看次数

JS - 如何访问同一文件中的 module.exports 函数

我有一个看起来像的函数:

module.exports = myFunction() {
  //do some stuff
}
Run Code Online (Sandbox Code Playgroud)

我可以使用其他文件很好地访问它 var myFunction = require(.thepath.js)

但是,我如何从它创建的文件中访问它。

我试过了myFunction()this.myFunction()但都不起作用。

谢谢

javascript node.js

6
推荐指数
1
解决办法
4318
查看次数

了解NodeJS中的导出

我不认为我完全理解Node.js中的exports工作方式.在一些示例代码中,我注意到以这种方式使用的对象:exports

exports = mongoose = require('mongoose')
mongoose.connect(config.db.uri)
exports = Schema = mongoose.Schema
Run Code Online (Sandbox Code Playgroud)

你使用exports =两次这样的幕后发生了什么?在我看来,"mongoose"不应该被导出.我做了这个快速测试:

var foo
  , bar

exports = foo = 'foo'
exports = bar = 'bar'

// reports 'bar' only
console.log(exports)
Run Code Online (Sandbox Code Playgroud)

第二次测试会覆盖第一次导出.

node.js

5
推荐指数
1
解决办法
8951
查看次数

Appcelerator和CommonJS模块(缓存和循环引用)

这是事情:

我正在使用CommonJS方式使我的移动(iPhone/Android)应用程序模块化.这并不奇怪.但有一点我无法理解.

CommonJS让我创建了STATIC私有变量,这让我可以轻松创建单例.至少,我认为这是因为获取require()d 的文件的内容只读取一次,然后每次都返回export对象(只初始化一次).

但是当我创建如下所示的循环引用时,每次都会执行包含模块内的代码.

等等...... 有趣的是,在我写这个问题的时候,我突然意识到require()在下一个问题开始之前没有任何调用完成(因此下面显示了堆栈溢出).

关于我是否正常的任何想法?这是凌晨5点过来的所以,所以我所关注的所有赌注都是关闭的:D.

例子:

看到这段代码,它定义了一个单例:

/* Singleton.js */

exports.getSingleton = getSingleton;

function getSingleton(name) {
  if (!instance) {
    instance = new Thing(name);
  }

  return instance;
}

function Thing(name) {
  this.name = name;
}

var instance;
Run Code Online (Sandbox Code Playgroud)

require()这个文件是这样的:

var theFirstThing = require('Singleton').getSingleton('first');
Ti.API.info('first: ' + theFirstThing.name)

var possiblyAnotherOtherThing = require('Singleton').getSingleton('second');
Ti.API.info('second: ' + possiblyAnotherOtherThing.name);
Run Code Online (Sandbox Code Playgroud)

输出是:

[DEBUG] loading: /path/to/sim/MyApp.app/app.js, resource: app_js
[DEBUG] loading: /path/to/sim/MyApp.app/Singleton.js, resource: Singleton_js
[INFO] first: first
[INFO] second: first
Run Code Online (Sandbox Code Playgroud)

为什么那样的循环引用如下所示不起作用?(如果你愿意的话,我可能已经自己研究了这个,评论/回答). …

titanium circular-reference appcelerator commonjs

4
推荐指数
1
解决办法
1926
查看次数

使用Express服务HTML

我有一个HTML文件(privacy.html),我想将其用作主页。我写了以下内容:

app.get('/', (req, res) => {
  res.writeHead(200, {'Content-Type': 'text/html'})
  res.write(require('./privacy.html'))
  res.end()
})
Run Code Online (Sandbox Code Playgroud)

怎么了?

node.js express

3
推荐指数
2
解决办法
5807
查看次数

node.export中的module.exports = function的含义

我正在使用node.js迈出第一步,显然我尝试做的第一件事就是从模块中导出一些数据,所以我尝试了这个简单的例子:

dummy.js:

var user = "rally";

module.exports = {
  user:user
};
Run Code Online (Sandbox Code Playgroud)

而不是从这样的不同文件中要求它:

var dummy = require('./dummy.js');
console.log(dummy.user); // rally
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,每件事都有效,但现在我潜入了代码,在模块的开头有这个定义:

module.exports = function(passport,config, mongoose) {}
Run Code Online (Sandbox Code Playgroud)

我不明白它的含义是什么,我怎么能用它.只是为了试图理解我在这个抽象函数中定义了一些变量但是无法从任何其他文件中获取它们的值.任何想法如何从像这样定义的模块中导出变量..?所以例如我可以要求这个模块并获取"Dummy"变量并在不同的文件中使用它

module.exports = function(passport,config, mongoose) {
var dummy = "Dummy";
}
Run Code Online (Sandbox Code Playgroud)

javascript node.js

2
推荐指数
1
解决办法
3924
查看次数

从Angular Controller调用节点函数的常用方法

让我以前缀为Node/Express更新.

我有一个AngularJS应用程序利用Node.JS来管理Azure Blob要求,例如创建Blob容器,如下所示:

function test(containerName) {
blobSvc.createContainerIfNotExists(containerName, function (error, result, response) {
    if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
    }
});
};

test('blob4');
Run Code Online (Sandbox Code Playgroud)

从Node中的server.js执行时创建容器的功能按预期工作并创建一个blob容器.但是,我需要在我的AngularJS应用程序中单击创建一个blob容器.我设想使用导出来访问和执行在Server.js中创建的函数,但是已经看到一些混合信息,特别是当Express.js在图片中时,用于通过AngularJS客户端调用Node.js函数,因为它似乎在Angular App中必须进行http调用(请参阅本文中的最后一个答案:来自angular应用程序的nodejs中的调用函数).

我的问题如下:

1)由于我的应用程序当前使用Node,Express和Angular,我是否需要在Angular控制器中使用http运行Node函数/执行Node/Server中编写的所有函数.如果通过AngularJS客户端调用,则需要$ http执行即使他们不打电话给服务,但可能是执行诸如数学之类的功能?基于Express的呼叫示例:

function MyCtrl($scope, $http) { 
// $http is injected by angular's IOC implementation

// other functions and controller stuff is here...    

// this is called when button is clicked
$scope.batchfile = function() {
    $http.get('/performbatch').success(function() {
        // url …
Run Code Online (Sandbox Code Playgroud)

node.js express angularjs

2
推荐指数
1
解决办法
2万
查看次数

无法在带有require的webpack中使用自定义函数

我正在使用react和webpack创建一个渐进的Web应用程序.我已成功配置所有内容并能够开始开发.现在,我有许多辅助函数,如:

function getCookie(name) {
      var start = document.cookie.indexOf(name + "=");
      var len = start + name.length + 1;
      if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
      }
      if (start == -1) return null;
      var end = document.cookie.indexOf(';', len);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(len, end));
}
Run Code Online (Sandbox Code Playgroud)

所以,为此我创建了另一个js文件:helper.jsx.现在我的helper.js包含上面的函数.现在我想在另一个反应组件中使用上述函数.

我在我的组件中做了一个要求:

var helper = require("helper");
Run Code Online (Sandbox Code Playgroud)

并尝试使用以下方法调用该函数:

helper.getCookie('user');
Run Code Online (Sandbox Code Playgroud)

哪个给了我helper.getCookie不是一个定义的.请告诉我如何创建一个帮助器js并在我的react组件中使用helper js的功能.

require reactjs webpack

1
推荐指数
1
解决办法
580
查看次数

NodeJs - [Restful API] 如何在不同的js文件中分离app的get方法?

我是 Node.js 新手。目前我正在帮助我的团队使用 Nodejs 开发一个 Restful api。我想问是否必须在单个文件中实现所有app.get方法和业务逻辑?

例如我的server.js:

var express = require('express')
var bodyParser = require('body-parser')
var app = express()
let family = {}

app.use(bodyParser.json())

app.route('/family')
  .get(function (request, response) {
    response.json(family)
  })
  .post(function (request, response) {
    family[request.body.isbn] = {
      name: request.body.name,
      relationship: request.body.relationship,
      age: request.body.age
    }

    response.json({message: 'Success'})
  })

app.listen(8080, function () {
  console.log('Server is started')
});
Run Code Online (Sandbox Code Playgroud)

如果我想构建一个名为 family.js 的文件并将所有与 family 相关的逻辑从 server.js 移至内部,该怎么办?

以及如何从 server.js 调用它?

有人可以帮忙吗?谢谢。

javascript api rest node.js

1
推荐指数
1
解决办法
2510
查看次数

throw new TypeError('app.use() 需要中间件函数') - 表达应用程序错误

我找不到问题出在哪里,nodejs 控制台也没有说太多。

错误:

TypeError: app.use() 需要 EventEmitter.use (C:\Users\Ezequiel\Desktop\mobile_standard\server\StandardServer\node_modules\express\lib\application.js:210:11) 中对象的中间件函数。(C:\Users\Ezequiel\Desktop\mobile_standard\server\StandardServer\app.js:21:5) 在 Module._compile (module.js:409:26) 在 Object.Module._extensions..js (module.js) :416:10) 在 Module.load (module.js:343:32) 在 Function.Module._load (module.js:300:12) 在 Function.Module.runMain (module.js:441:10) 启动时(node.js:139:18)在node.js:968:3

app.js(第 21 行 -> app.use(require('./controllers/login'));)

const express = require('express');
const cors = require('cors');
const bodyParser= require('body-parser');
const MongoClient = require('mongodb').MongoClient
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken'); 
const app = express();

var config = require('./config'); 
var User = require("./models/user"); 

app.use(cors());
app.options('*', cors());
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

mongoose.connect(config.database); 
app.set('superSecret', config.secret);

app.use(require('./middlewares/auth'));
app.use(require('./controllers/login'));    

app.get('/normal', function(req,res){
    res.json({ …
Run Code Online (Sandbox Code Playgroud)

node.js express

1
推荐指数
1
解决办法
2673
查看次数

在定义foo之前如何使'module.exports = foo'工作?

我不会声称我非常精通Node,甚至是Javascript,但我已经看到了几个模块的形式

module.exports = foo;

function foo() {
  ...
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以看到这种情况可能在这种情况下有效,但当模块返回一个被执行的函数时,我真的很困惑.

module.exports = bar();

function bar() {
  ...
}
Run Code Online (Sandbox Code Playgroud)

这巫术是什么?

javascript node.js

0
推荐指数
1
解决办法
89
查看次数