如何将这个PHP转换为express.js/node
How to convert this PHP to express.js/node
>我正在尝试创建一个端点,该端点获取发布数据并将其保存到png文件中。这个 PHP 代码是这样做的:
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);
// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);
// Save file. This example uses a hard coded filename for testing,
// but a real application can specify filename in POST variable
$fp = fopen( 'test.png', 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
}
我是新来表达的,我有这个:
app.use (function(req, res, next) {
var data='';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
app.post('/upload', function(req, res){
var testData = req.body;
return res.send(testData);
});
我得到一个空白对象。 即使实际数据正在发布。有人可以告诉我一种用 express 编写上述代码的好方法吗?
谢谢
所以从处理程序那里获取它:
var fs = require('fs');
app.post('/upload', function(req, res){
var image = req.body;
var noHeader = image.substring(image.indexOf(',') + 1);
var decoded = new Buffer(noHeader, 'base64');
fs.writeFile('testfile.png', decoded, function(err){
res.send('done!');
});
});
相关文章:
- renderReact/Rect Router+Node/Express.js的服务器端/同构渲染中未定义renderP
- Express.js:在用户发帖时获取用户的引荐来源网址
- 使用connect vhost为多个express.js应用程序提供服务
- Express.js和multer:如何知道文件何时全部上传
- Node.JS, Angular.JS, MySql,Express.JS
- Express.js应用程序最大CPU
- Electron:Express.js服务器在Mongo数据库'It’准备好了
- express js 4.4.1中的压缩文件(gzip)
- 缺少Express JS-jwt符号exp
- express js中具有不同功能的动态路由
- 使用React.js+Express.js发送电子邮件
- 在express.js中为每个请求设置全局res.local变量
- 为什么Express.js后端的CORS设置不起作用
- 修改express.js中的res对象
- fs.readFileSync()在express js应用程序中失败
- 如何在Express JS中使用Bliss View Engine而不是Jade
- 猫鼬连接在 Express.js 应用程序中自动共享
- 如何使用 express.js 和 ember-cli 实现节点.js后端服务器
- Express JS-查看/检查响应对象
- 如何在设置后将 Express.js 用于新的 Web 应用