从Node.js中的请求方法中读取

时间:2022-01-20 16:54:38

I'm writing a Node.js script that converts HTML files to ENML (Evernote Markup Language).

我正在编写一个Node.js脚本,将HTML文件转换为ENML(Evernote标记语言)。

Now this script correctly converts an existing HTML file to the desired ENML output. Now, I have the following question:

现在,此脚本正确地将现有HTML文件转换为所需的ENML输出。现在,我有以下问题:

Client will be sending an HTML file in JSON format. How do I listen for all incoming requests, take the JSON object, convert to ENML, and write back the response to the original request?

客户端将以JSON格式发送HTML文件。如何侦听所有传入请求,获取JSON对象,转换为ENML,并将响应写回原始请求?

My code for this is as follows:

我的代码如下:

var fs = require('fs');
var path = require('path');
var html = require('enmlOfHtml');
var contents = '';
var contents1 = '';
fs.readFile(__dirname + '/index.html', 'utf8', function(err, html1){
    html.ENMLOfHTML(html1, function(err, ENML){ //using Enml-js npm
        contents1=ENML;
    });
});
var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'application/json'});
      res.write(contents1);
    }).listen(4567, "127.0.0.1");    

Thanks!

2 个解决方案

#1


1  

I guess that the client will make POST requests to your server. Here is how you could get the send information:

我想客户端会向你的服务器发出POST请求。以下是获取发送信息的方法:

var processRequest = function(req, callback) {
    var body = '';
    req.on('data', function (data) {
        body += data;
    });
    req.on('end', function () {
        callback(qs.parse(body));
    });
}

var http = require('http');
http.createServer(function (req, res) {
    processRequest(req, function(clientData) {
        html.ENMLOfHTML(clientData, function(err, ENML){ //using Enml-js npm
            contents1 = ENML;
            res.writeHead(200, {'Content-Type': 'application/json'});
            res.write(JSON.stringify(contents1));
        });
    }); 
}).listen(4567, "127.0.0.1");

#2


0  

You can use the Node's request module.

您可以使用Node的请求模块。

request('http://www.example.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

#1


1  

I guess that the client will make POST requests to your server. Here is how you could get the send information:

我想客户端会向你的服务器发出POST请求。以下是获取发送信息的方法:

var processRequest = function(req, callback) {
    var body = '';
    req.on('data', function (data) {
        body += data;
    });
    req.on('end', function () {
        callback(qs.parse(body));
    });
}

var http = require('http');
http.createServer(function (req, res) {
    processRequest(req, function(clientData) {
        html.ENMLOfHTML(clientData, function(err, ENML){ //using Enml-js npm
            contents1 = ENML;
            res.writeHead(200, {'Content-Type': 'application/json'});
            res.write(JSON.stringify(contents1));
        });
    }); 
}).listen(4567, "127.0.0.1");

#2


0  

You can use the Node's request module.

您可以使用Node的请求模块。

request('http://www.example.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});