我的个人网页托管在没有节点的dever中。
我在节点服务器(Heroku(上运行我的服务器端脚本。
我正在尝试从我的服务器JS文件中读取个人页面的内容...
这是服务器代码
server.js:
const express = require("express");
const app = express();
const fs = require('fs');
var http = require("http");
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + "/public"));
fs.readFile("http://myportfolio.maxxweb.com", (err, data) => {
if (err) {
console.log(err)
} else {
console.log("Loaded personal page")
}
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/main.html");
});
app.get("/api/names", (req, res) => {
console.log("names api call... ");
});
app.get('/*', (req, res) => {
res.redirect('/error.html')
});
app.get("/get-portfolio-page-content", (req, res) => {
var options = {
host: 'http://myportfolio.maxxweb.com'
};
http.get(options, function(http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function(chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function() {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
});
app.listen(port, () => {
console.log(`Server running at port ` + port);
})
在将代码倾斜到Heroku之前,我从我的本地运行命令> node server.js 。我收到以下错误:
Server running at port 3000
{ [Error: ENOENT: no such file or directory, open 'C:maxxpcprojectherokuhttp:myportfolio.maxxweb.com']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\maxxpc\project\heroku\http:\myportfolio.maxxweb.com' }
我是节点环境的新手。有人请指导我。
fs
模块用于与文件系统进行交互,而不是为了制作http请求。
您需要使用设计用于制作HTTP请求的模块,例如Axios或内置的HTTP和HTTPS模块。