我是 node.js 的新手,正在構建一個在現有 PHP 應用程式中運行的聊天應用程式。我正在使用 expressjs 和 postman-request 模塊。我可以輕松訪問絕對 url,但是當我嘗試請求位于我自己的檔案系統上的檔案時,它失敗了。我觀看了教程并閱讀了檔案,似乎唯一展示過的示例是如何訪問外部 URL。我無法想象無法訪問駐留在您自己的檔案系統上的檔案。
這是下面的代碼:(在我的主 index.js 服務器檔案中)
const request = require('postman-request');
const url = 'utils/config.php'; // this file merely echos out a json encoded string.
request(url, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response
console.log('body:', body);
});
這是錯誤訊息:錯誤:錯誤:無效的 URI“utils/config.php”
這是檔案結構:
-node_modules
-public
-src
-utils
-config.php
index.js (start for node.js - inside src folder)
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
請求是指從網路服務器請求檔案。在這里,您嘗試請求一個檔案路徑,而計算機認為是一個不存在的 URL。PHP 檔案只是文本。它對計算機沒有任何意義,PHP 解釋器是運行 PHP 代碼的工具。在這種情況下,您沒有運行 PHP 服務器,您請求的是檔案路徑,而不是 URL。
您必須首先使用該php命令啟動 PHP 服務器。確保傳入 URL(例如,localhost:8080/config.php),而不是檔案路徑。
如果您想完全從 Node 腳本執行此操作,您可以啟動服務器,請求 URL,然后停止服務器。可以使用spawn內置child_process模塊中的函式(請參閱此處)。您也可以使用exec,但這可能不必要地更難。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/328946.html
標籤:节点.js
