Javascript 如何在nodejs中包含非js文件,如test.cookie
我知道在nodejs中包含文件的通常方法是使用Javascript 如何在nodejs中包含非js文件,如test.cookie,javascript,node.js,Javascript,Node.js,我知道在nodejs中包含文件的通常方法是使用 var myFile=require(“./file”) 但是,如果文件包含“#”或TRUE或FALSE等符号,nodejs将失败,并出现以下错误: SyntaxError: Unexpected token ILLEGAL at Module._compile (module.js:437:25) at Object.Module._extensions..js (module.js:467:10) at Module.l
var myFile=require(“./file”)代码>
但是,如果文件包含“#”或TRUE或FALSE等符号,nodejs将失败,并出现以下错误:
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
(function (exports, require, module, __filename, __dirname) { #
有没有办法在nodejs中包含此类文件。正如昆汀所提到的,“require”用于加载模块。您可能需要做的是读取文件
请参阅。文件内容随后在您指定的回调中可用
您可能还需要参考以下内容
readFile示例
require
是加载JavaScript模块的正常方式。我不知道你说的“包含文件”是什么意思。我试图加载的文件是一个名为test.cookie的cookie。我需要在curl命令curl中使用这个文件-b test.cookie…
定义“包括”。我想不出它的任何定义是在系统调用中使用它的路径的先决条件。当节点有自己的HTTP库时,为什么要考虑对curl进行系统调用?问题是,我在使用https.request时看到了相同的错误。问题出在帖子标题上<代码>var postheaders={'Content Type':'application/json','Content Length':Buffer.byteLength(jsonObject,'utf8'),'Cookie':test.Cookie}代码>因为该代码不调用require
,所以这似乎不太可能<代码>如果(错误)抛出错误;错误:eNote,打开“test.cookie”
您必须将零件正确地放入文件中。谢谢大家。我知道问题出在哪里了。我需要在使用exec在NodeJS中执行的curl命令中指定cookie的完整路径。
var fs = require('fs');
var cookie;
fs.readFile("/path/to/data", (error, data) =>{
//do stuff with data which is of type Buffer
cookie = data.toString();
})