[英]Read from request method in Node.js
I'm writing a Node.js script that converts HTML files to ENML (Evernote Markup Language). 我正在编写一个将HTML文件转换为ENML(Evernote标记语言)的Node.js脚本。
Now this script correctly converts an existing HTML file to the desired ENML output. 现在,此脚本可以将现有的HTML文件正确转换为所需的ENML输出。 Now, I have the following question: 现在,我有以下问题:
Client will be sending an HTML file in JSON format. 客户端将以JSON格式发送HTML文件。 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对象,转换为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! 谢谢!
I guess that the client will make POST requests to your server. 我想客户端将向您的服务器发出POST请求。 Here is how you could get the send information: 您可以通过以下方式获取发送信息:
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.