Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 打开特定页面问题的客户端http请求_Javascript_Node.js - Fatal编程技术网

Javascript 打开特定页面问题的客户端http请求

Javascript 打开特定页面问题的客户端http请求,javascript,node.js,Javascript,Node.js,我正在尝试发出http请求并在浏览器中打开特定页面。我尝试过几个软件包,比如npm opn、npm open、openurl,但我没有达到预期的目标。以下是我正在尝试的: server.js var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var http = require('http'); var fs = require('fs'); var u

我正在尝试发出http请求并在浏览器中打开特定页面。我尝试过几个软件包,比如npm opn、npm open、openurl,但我没有达到预期的目标。以下是我正在尝试的:

server.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var http = require('http');
var fs = require('fs');

var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(express.static('public'));

app.get('/index.html', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
})

app.post('/process_post', urlencodedParser, function (req, res) {

   response = {
      fname:req.body.fname,
      surname:req.body.surname,
      age:req.body.age,
      phone:req.body.phone,
      city:req.body.phone,
      email:req.body.email,
      country:req.body.country,
      postal_code:req.body.postal_code,
      password:req.body.password
   };
   console.log(response);
   res.end(JSON.stringify(response));

   var exportJson= JSON.stringify(response);

   fs.writeFile("input.txt", exportJson, function(err) {
    if(err) {
        return console.log(err);
    }
});

}) 

var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("Listening at http://%s:%s", host, port)

})
client.js

var http = require('http');
var opn=require('opn');

// Options to be used by request 
var options = {
   host: '127.0.0.1',
   port: '8081',
   path: '/index.html'  
};

//opn("https://google.pt");
 var callback = function(response){
   // Continuously update stream with data
   var body = '';
   response.on('data', function(data) {
      body += data;
   });

   response.on('end', function() {
      console.log(body);
   });
}
var req = http.request(options, callback);
req.end();
编辑:

我需要通过连接到服务器/本地主机的客户端文件打开浏览器(页面是一个表单)。使用opn之类的NPM打开文档,如下所示:

opn(__dirname + "/" + "index.html"); //this line in the client.js
我想要这样的东西:

var req = http.request(options, callback);
opn(options.path);//with this line, when running the client.js (with the server on), the program opens another command prompt...
req.end();

你想重定向到另一个页面吗?我真的不明白你想做什么

您可以使用:
res.redirect('example.com')

编辑: 哦,好的,我想我已经理解了,你想在你的控制台中用node.js打开浏览器,是这样吗

编辑2: 使用


试试这个。

你想重定向到另一个页面吗?我真的不明白你想做什么

您可以使用:
res.redirect('example.com')

编辑: 哦,好的,我想我已经理解了,你想在你的控制台中用node.js打开浏览器,是这样吗

编辑2: 使用



试试这个。

代码>opn(options.path)不起作用,因为您应该使用完整路径调用
opn
,即
opn('http://127.0.0.1:8081/index.html)

opn(options.path)不起作用,因为您应该使用完整路径调用
opn
,即
opn('http://127.0.0.1:8081/index.html“)

如果需要打开浏览器,为什么要发出请求?我不能发出请求并根据该请求打开浏览器吗?为什么需要这样做?如果您想打开
/index.html
,只需使用
opn
。或者你想得到什么。请用这段代码说明清楚,添加你建议的行,其他控制台(cmd)打开。回答了Shigehiro Kamisama建议我进入客户端控制台“found.Redirecting to index.html”,但浏览器无法打开。我尝试这样做是因为我想做测试
opn(options.path)不起作用,因为您应该使用完整路径调用
opn
,即
opn('http://127.0.0.1:8081/index.html“)
如果需要打开浏览器,为什么要发出请求?我不能发出请求并根据该请求打开浏览器吗?为什么需要这样做?如果您想打开
/index.html
,只需使用
opn
。或者你想得到什么。请用这段代码说明清楚,添加你建议的行,其他控制台(cmd)打开。回答了Shigehiro Kamisama建议我进入客户端控制台“found.Redirecting to index.html”,但浏览器无法打开。我尝试这样做是因为我想做测试
opn(options.path)不起作用,因为您应该使用完整路径调用
opn
,即
opn('http://127.0.0.1:8081/index.html“)
是的,您之前的回答只是让我重定向了控制台:(“找到了。重定向到index.html”)…我需要通过app-connection client serverTest with Child process在浏览器中打开页面我认为您可以使用Child\u process运行cmd命令,以便使用edit打开浏览器:var req=http.request(选项,回调);const exec=require('child_process')。exec;exec('explorer'+options.path,(error,stdout,stderr)=>{if(error)返回控制台。error(
error:${error}
)});请求结束();就像这样,程序打开了我的文档文件夹…我不明白,奇怪…我已经重新编辑了我的代码,现在试试这个,我想这个可以工作。使用您的代码,您试图打开一个本地文件,因此需要像加载网站一样加载它,然后使用127.0.0.1:8081是的,您以前的回答只是让我重定向控制台:(“find.Redirecting to index.html”)…我需要通过app-connection client serverTest with Child process在浏览器中打开页面我认为您可以使用Child\u process运行cmd命令,以便使用edit打开浏览器:var req=http.request(选项,回调);const exec=require('child_process')。exec;exec('explorer'+options.path,(error,stdout,stderr)=>{if(error)返回控制台。error(
error:${error}
)});请求结束();就像这样,程序打开了我的文档文件夹…我不明白,奇怪…我已经重新编辑了我的代码,现在试试这个,我想这个可以工作。在代码中,您试图打开一个本地文件,因此需要像加载网站一样加载它,然后使用127.0.0.1:8081
var req = http.request(options, callback);
const exec = require('child_process').exec;
exec('explorer 127.0.0.1:8081'+options.path, (error, stdout, stderr) => { 
  if (error) return console.error(error : ${error})
})
req.end();