【发布时间】:2019-05-13 08:02:16
【问题描述】:
我的个人网页托管在没有节点的服务器中。
我的服务器端脚本在节点服务器 (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:\maxxpc\project\heroku\http:\myportfolio.maxxweb.com']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\\maxxpc\\project\\heroku\\http:\\myportfolio.maxxweb.com' }
我是 Node 环境的新手。有人请指导我。
【问题讨论】:
-
必须查看文档。第一点你不会工作:nodejs.org/api/fs.html#fs_url_object_support > 对于大多数 fs 模块函数,路径或文件名参数可以作为 WHATWG URL 对象传递。仅支持使用
file:协议的 URL 对象。
标签: javascript node.js express