node.js中require(...)
的參數是一個文件名。如果我有一個模塊源代碼字符串code
,我可以以某種方式調用require(code)
並從該字符串中加載函數?在nodejs中需要
回答
解決辦法可能是將模塊源代碼寫入臨時文件./tmp-file.js
,然後require('./tmp-file')
,然後刪除該文件。
這可能不是最優的,因爲您要麼必須同步地阻止和寫入文件,要麼將所有需要該模塊的內容都放入異步寫回調中。
用於異步文件寫入(gist - also includes sync file write)工作的示例:
var http = require('http');
var fs = require('fs');
var helloModuleString = "exports.world = function() { return 'Hello World\\n'; }";
fs.writeFile('./hello.js', helloModuleString, function (err) {
if (err) return console.log(err);
var hello = require('./hello');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(hello.world());
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
});
結果:
$ curl 127.0.0.1:1337
> Hello World
謝謝,聽起來像一個很好的基準解決方案。 – Cartesius00
總的來說,我同意,但這需要允許節點應用程序在文件系統上創建文件。如果你啓動節點應用程序的多個實例,你必須以某種方式決定如何處理文件或命名空間。我試圖添加一個自定義的擴展,但現在已經棄用。沒有其他好的解決方案嗎? –
我把這個變成一個功能重用。它根據隨機哈希在os臨時目錄中創建一個文件,需要它然後刪除它。
var fs = require('fs'),
os = require('os'),
crypto = require('crypto');
function requireString(moduleString) {
var token = crypto.randomBytes(20).toString('hex'),
filename = os.tmpdir() + '/' + token + '.js',
requiredModule = false;
// write, require, delete
fs.writeFileSync(filename, moduleString);
requiredModule = require(filename);
fs.unlinkSync(filename);
return requiredModule;
}
然後,你可以這樣做:
var carString = "exports.start = function(){ console.log('start'); };",
car = requireString(carString);
console.log("Car:", car);
這是更一種解決辦法,但使用起來比較方便,我想。
不錯,謝謝 – Cartesius00
- 1. 我需要在點的NodeJS
- 2. 在nodejs frappe需要路徑
- 3. NodeJs需要錯誤
- 4. Nodejs - 需要說明
- 5. Apache需要NodeJs嗎?
- 6. 如何在nodejs中需要類
- 7. 級聯需要在NodeJs中,需要一個需要另一個文件的文件NodeJs
- 8. NodeJS - 需要錯誤密碼
- 9. NodeJS原型和需要
- 10. 需要的NodeJS配置
- 11. NodeJS需要範圍變量
- 12. NodeJs需要('./ file.js')問題
- 13. RESTful MongoDB需要Nodejs示例
- 14. 的jQuery的NodeJS需要jsdom
- 15. +的NodeJS :: sequelize包括在需要假
- 16. 需要的NodeJS(「本」),(即)在打字稿
- 17. nodejs需要返回空對象
- 18. 需要的NodeJS()時產生的錯誤
- 19. 爲什麼angular2需要nodejs 5
- 20. nodejs oauth2需要令牌重建幫助
- 21. 需要SSL證書的Nodejs/Socketio級別
- 22. 需要的NodeJS不起作用
- 23. 和的NodeJS需要包括index.js文件
- 24. 需要多個API文件的NodeJS
- 25. 避免的NodeJS模塊「需要」複製
- 26. NodeJS需要('./ path/to/image/image.jpg')作爲base64
- 27. NodeJS需要永久構建AWS EC2
- 28. heroku nodejs部署錯誤,需要模塊
- 29. NodeJS TypeError('JwtStrategy需要密鑰或密鑰');
- 30. 出口的NodeJS功能上需要
http://nodejs.org/api/modules.html#modules_modules(什麼是荒謬的uri) – jAndy
@jAndy哪裏是一個字符串中的源代碼?我只看到從文件加載。 – Cartesius00
http://nodejs.org/api/modules.html – Thomas