Tag: 需要

Nodejs的res没有被定义在内部需求

对于我的api我想要一个单独的文件来设置一个cookie(使用cookieparsing器)。 但res或req不会传递给所需的文件… index.js app.get('/api/user/:username', function(req, res) { urlUsername = req.params.username; require('./set/cookie') }); 集/ cookie.js res.cookie('login_session', urlUsername) // returns 'res' not defined 正如你所看到的部分解决这个问题,我设置了urlUsername工作。 但肯定还有另一种方式:)? 谢谢

要求不适用于节点opcua

我想在HTML文件中加载一个带有“require”的节点opcua的本地版本,但实际上并不成功。 代码片段如下: <script type="text/javascript" src="path_to_require.js"></script> <script> var opcua = require(["path_to_node-opcua"]); <!– Yes, the path is correct > var client = new opcua.OPCUAClient(); … 当我执行脚本时,在控制台中出现以下错误: Uncaught TypeError: opcua.OPCUAClient is not a constructor 因此, var opcua加载正确,但OPCUACluent不是,尽pipe该类声明在一个文件中,该文件存在于opcua_client.js node-opcua\lib\client\下的node-opcua\lib\client\ 来源: http ://requirejs.org/docs/download.html#requirejs中的“require”脚本。 使用console命令npm install node-opcua的节点opcua文件夹。

URL不是一个函数node.js

我一直在尝试使用这里描述的节点js的url模块: https : //nodejs.org/api/url.html#url_url ,但我似乎无法得到简单的例子工作。 我想能够build立一个url,但与下面的代码, const URL = require('url'); const apiUrl = new URL('myapi.com/'); apiUrl.searchParams.set('tickers', tickers); console.log(apiUrl.href); 我遇到了错误,该URL不是一个函数。 我已经尝试了与网站build议初始化URL的其他方式,如 const { URL } = require('url'); 但是,这会导致第一个打开的括号中的语法错误。 我如何使用url模块来实际操作url? 编辑: 根据MinusFour的build议,我现在到了这里: const URL = require('url').Url; const apiUrl = new URL('myapi.com/'); console.log(apiUrl); apiUrl.search = 'tickers=[\'msft\']'; console.log(apiUrl.href); 这确实编译和运行,而不会引发错误,但日志 Url { protocol: null, slashes: null, auth: null, host: null, […]

node.js需要Object.prototype?

我有点困惑,node.js require系统。 我知道如何export或require d组件的module.export函数,但不知道如何增强与原型js对象。 例如,我有一个代码,以通过prototype.watch方法来增强js Object。 我将在我自己的npm包中包含这些代码,并使用node require导入。 什么是最好的方式来做到这一点? 我需要重构这个代码作为一个function导出,需要执行后require吗? 谢谢。 if (!Object.prototype.watch) { Object.defineProperty(Object.prototype, "watch", { enumerable: false, configurable: true, writable: false, value: function(prop, handler) { var oldval = this[prop], newval = oldval, getter = function() { return newval; }, setter = function(val) { oldval = newval; return newval = handler.call(this, prop, oldval, val); […]

使用require将variablesdynamic分配给全局范围

我正在构build一个模块化的应用程序,并根据用户的喜好有一个configuration文件,以包括不同的模块 通常我会做类似的事情 var fs = require('fs'); 但我从数组中获取所需的模块,所以对于每个需求我有一个脚本对象,看起来像这样 { name:'fs', file:'fs', isGlobal:true } 然后我dynamic地请求模块 window[script.name] = require(script.file); 这工作正常,如果我检查window.fs 。 但是,依赖于fs其他模块只能调用fs 。 我知道window.fs和fs都应该解决,但是在node-webkit中,它们不是。 我试图找出一种方法来将var name直接包含在全局中。 当然,我不能使用var script.name = require(script.file); 因为这将设置script.name值,而不是全局variables。 对此有何build议?

当执行一个Function对象时,节点“ReferenceError:require is not defined”

我试图执行一个函数对象,它基本上和下面的伪代码一样: var testF = new Function("x","y", "var http = require('http');"); testF('foo','bar'); 并得到: ReferenceError: require is not defined 我是否需要以某种方式添加重新加载require模块的内容,因为它不是Node中的全局模块? 如果是这样,谷歌并不是我的朋友,所以任何关于如何这样做的指针会是太棒了。 感谢您的想法。

节点需要path引用问题

我正在使用节点来运行一些JS任务。 我正在使用node require来包含我写的其他需要的文件。 我想包括的文件存在于同一个目录中。 这些文件没有什么特别之处。 只是普通的旧JavaScript。 我没有把内容提供给浏览器或任何东西。 我只是想运行JS的逻辑。 我的目录结构: 根 演习 EX1 FileLoader.js FileOne.js 在我的FileLoader.js中,我自己运行了这些文件path模式中的每一个。 他们是在我的企图顺序。 require('./exercises/ex1/FileOne.js'); // Attempt 1 path require('FileOne.js'); // Attempt 2 path require('/exercises/ex1/FileOne.js'); // Attempt 3 path require('../../exercises/ex1/FileOne.js'); // Attempt 4 path terminal命令: node ./exercises/ex1/ex1.js 成果: 尝试1 – 错误:找不到模块'./exercises/ex1/FileOne.js' 尝试2 – 错误:无法find模块“FileOne.js” 尝试3 – 错误:找不到模块'/exercises/ex1/FileOne.js' 尝试4 – 它的工作,即使我没有想到…. FML 所以最后一个工作,但所需的path是非常愚蠢的。 这似乎是我的应用程序没有根的概念。 […]

如何在Node.js中要求GSAP

我尝试在Node.js中包含GSAPanimation库 例如,我尝试运行带有完整function的GSAP,并且需要先包含“TweenMax.js”,然后再包含“TimelineMax.js”。 在GSAP的最后一个版本中,我看到'TweenMax.js'和'TimelineMax.js'在代码实现中有Node.js的module.exports,但是当我试图用Node.js来要求它们时 – require('./lib/gsap/TweenMax.js') ,Node.js返回我 'reference error: document is not defined' 在TweenMax.js中打破所有的脚本。 如果有任何想法如何解决这个问题,请帮助。 谢谢。

Javascript“require()”方法

在python中,当你导入一个模块时,导入模块的'if name == main '块内部的语句不会被执行。 有没有什么等效的方法可以防止在JavaScript中导入的模块中执行不需要的语句?

Node.js,要求文件夹中的所有模块直接使用加载的模块

在MyModule文件夹中,我有这两个JS文件。 SayHello.js module.exports.SayHello = function() { return('Hello !'); } SayByeBye.js module.exports.SayByeBye = function() { return('Bye Bye!'); } 在Node.js中,我想要求MyModule文件夹中的所有文件,并直接调用SayHello & SayByeBye函数: require(./MyModule) console.log(SayHello()); console.log(SayByeBye()); 编辑: 用@ Yanick Rochon的回答,我这样做: > ./app/my-module/index.js global.SayHello = require('./my-module/SayHello').SayHello; global.SayByeBye = require('./my-module/SayByeBye').SayByeBye; > ./app/my-module/say-hello.js module.exports.SayHello = function() { return('Hello !'); }; > ./app/my-module/say-byebye.js module.exports.SayByeBye = function() { return('Bye Bye !'); }; > […]