[英]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.js
在localhost: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 theNodeServer.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.