节点:从外部网站(域)加载/读取页面内容 [英] Node: Load/read the content of a page from external website(domain)

查看:33
本文介绍了节点:从外部网站(域)加载/读取页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的个人网页托管在没有Node的服务器上.

I have my personal webpage hosted in a sever which does not have Node.

我在Node Server(Heroku)上运行了服务器端脚本.

I have my server-side script running on Node Server (Heroku).

我正在尝试从服务器js文件中读取个人页面的内容...

I am trying to read the content of my personal page from my server js file...

这是服务器代码

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 .我收到以下错误:

Before deplyoing code to Heroku, I run the command> node server.js from my local. I get the following error:

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环境的新手.有人请引导我.

I am new to Node environment. Someone please guide me.

推荐答案

fs 模块用于与文件系统进行交互,而不用于发出HTTP请求.

The fs module is for interacting with the file system, not for making HTTP requests.

您需要使用一个设计用于发出HTTP请求的模块,例如axios或内置的http和https模块.

You need to use a module designed for making HTTP requests such as axios or the built-in http and https modules.

这篇关于节点:从外部网站(域)加载/读取页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆