简体   繁体   English

节点:从外部网站(域)加载/读取页面内容

[英]Node: Load/read the content of a page from external website(domain)

I have my personal webpage hosted in a sever which does not have Node. 我的个人网页托管在没有Node的服务器上。

I have my server-side script running on Node Server (Heroku). 我在节点服务器(Heroku)上运行服务器端脚本。

I am trying to read the content of my personal page from my server js file... 我正在尝试从服务器js文件中读取个人页面的内容...

Here is the server code 这是服务器代码

server.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);
})

Before deplyoing code to Heroku, I run the command> node server.js from my local. 在将代码交付给Heroku之前,我从本地运行命令> node server.js 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' }

I am new to Node environment. 我是Node环境的新手。 Someone please guide me. 有人请指导我。

The fs module is for interacting with the file system, not for making HTTP requests. fs模块用于与文件系统进行交互,而不用于发出HTTP请求。

You need to use a module designed for making HTTP requests such as axios or the built-in http and https modules. 您需要使用一个设计用于发出HTTP请求的模块,例如axios或内置的http和https模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM