简体   繁体   English

使用脚本从网页检索数据,然后使用Node JS服务器访问数据

[英]Retrieving data from webpage using script and then accessing it with node js server

I have a html file index.html with a local script script.js running in localhost:8080 using a script NodeServer.js in Node.js . 我有一个HTML文件index.html其中有一个本地脚本script.jslocalhost:8080运行,该脚本使用Node.js NodeServer.js脚本。

I want that, whenever I enter data in input boxes, it should get fetched by the local script and then the script.js export it to the NodeServer.js application, which would further log the data on the console. 我希望,每当我在输入框中输入数据时,都应该通过本地脚本来获取数据 ,然后script.js会将其导出到NodeServer.js应用程序中,这将进一步在控制台上记录数据。

I am using Chrome browser - 65.0.3325.181 我正在使用Chrome浏览器65.0.3325.181

Here is my index.html : 这是我的index.html

 <!DOCTYPE html> <html> <head> <title>Website</title> <!-- My local script --> <script src='file:///F:/RahulVerma/NodeJS/LoginPage/script.js'></script> </head> <body> <input type='text' placeholder='Enter username' id='username'/><br/> <input type='password' placeholder='Enter password' id='password'/><hr/> <!-- getData() is present in local script --> <button onclick='getData();'>Submit</button> </body> </html> 

Here is my script.js : 这是我的script.js

 let getData = () => { let username = document.getElementById('username').value; let password = document.getElementById('password').value; module.exports = { username, password }; // exporting username and password to server running through node.js } 

And my NodeServer.js : 还有我的NodeServer.js

 const http = require('http'); // to create server const fs = require('fs'); // to read index.html const script = require('F:/RahulVerma/NodeJS/LoginPage/script'); // to fetch data from script.js // My Server let server = (request, response) => { fs.readFile('F:/RahulVerma/NodeJS/LoginPage/index.html', 'utf8', (error, result) => { if(error) { response.writeHead(404, {'Content-Type': 'text/plain'}); response.write(`Error: ${error}`); } else { response.writeHead(200, {'Content-Type': 'text/html'}); response.write(result); } response.end(); }); } // Creating server http.createServer(server).listen(8080); console.log(script.username, script.password); // logging data onto the console 

When I type node NodeServer.js in Node.js 9.3.0 (ia32) and npm, the index.html starts working on localhost:8080 but the script.js fails to render in the browser due to the following error: Not allowed to load local resource: file:///F:/RahulVerma/NodeJS/LoginPage/script.js 当我在Node.js 9.3.0(ia32)和npm中键入节点NodeServer.js时, index.html开始在localhost:8080上运行,但是由于以下错误, script.js无法在浏览器中呈现: Not allowed to load local resource: file:///F:/RahulVerma/NodeJS/LoginPage/script.js

What am I doing wrong ? 我究竟做错了什么 ?

cors error:a mechanism that uses additional HTTP headers to let a user agent gain permission to access selected resources from a server on a different origin cors错误:一种机制,该机制使用其他HTTP标头来让用户代理获得访问另一来源服务器上的选定资源的权限

// Create our server
var server;
server = http.createServer(function(req,res){
// Set CORS headers
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");;
if ( req.method === 'OPTIONS' ) {
    res.writeHead(200);
    res.end();
    return;
}
});

further clarification visit stack overflow cors 进一步说明访问堆栈溢出cors

Best awnser link 最佳遮阳篷链接

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

相关问题 使用“子进程”从 python 脚本中检索数据适用于普通 node.js 脚本,但不适用于必需的脚本 - Retrieving data from python script using 'child-process' works in normal node.js script, but not in a required script 在其他服务器上使用PHP脚本从数据库检索数据 - Retrieving data from database using PHP script on different server 如何使用node.js将表单数据从客户端脚本发送到服务器脚本 - How to send form data from client script to server script using node.js 如何防止用户直接在node.js中访问网页? - how to prevent user from accessing webpage directly in node.js? 从节点服务器访问js时出错 - Error accessing js from node server 从Node.js服务器检索数据并在Javascript客户端中循环时出错 - Error on retrieving data from Node.js server and cycling in Javascript client 从内容脚本访问网页脚本 - Accessing webpage script from content script 从api(request / node.js)检索数据并尝试使用d3显示 - Retrieving data from api (request/node.js) and trying to display it using d3 使用node.js将数据显示到从mongodb检索的网页上 - Display the data onto webpage retrieved from mongodb using node.js 使用相对于当前脚本的路径访问文件? (节点.js) - Accessing a file using path relative to current script? (Node.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM