Meteor.js REST API使用Postman发布图像数据
我正在为一个移动原生应用程序创建API,需要创建一个REST API来上传用户配置文件。但是当我试图在POST请求中上传图像时,它显示为未定义 这是我在app/server/server.js文件中的代码:Meteor.js REST API使用Postman发布图像数据,api,meteor,iron-router,Api,Meteor,Iron Router,我正在为一个移动原生应用程序创建API,需要创建一个REST API来上传用户配置文件。但是当我试图在POST请求中上传图像时,它显示为未定义 这是我在app/server/server.js文件中的代码: if(Meteor.isServer) { Router.route('/api/uploadpic/', { where: 'server' }) .post(function () { let response; console.log(this.req
if(Meteor.isServer) {
Router.route('/api/uploadpic/', { where: 'server' })
.post(function () {
let response;
console.log(this.request.body.file);
this.response.setHeader('Content-Type', 'application/json');
this.response.end(JSON.stringify(response));
});
}
花了很多时间后,我没有得到任何有用的东西。我需要用什么包装吗?
Postman api请求不确定是否有帮助,但尝试将内容类型从application/json更改为“multipart/form data”1)您必须创建一个express应用程序并与Meteor WebApp绑定
2) 用于在nodejs和meteor上上传照片
import { WebApp } from 'meteor/webapp';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import path from 'path';
const multer = require('multer');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('*', cors());
// use multer({ dest: 'uploads/' }) if you have desire folder
// I used memory storage here
// 'avatar' is the post request field name for the file
const uploader = multer({ storage: multer.memoryStorage() }).single('avatar');
app.post('/uploads', uploader, Meteor.bindEnvironment(async(req, res, done) => {
try {
//you can get the file in req.file
let fileName = req.file.originalname.replace(/\s/g, '_');
let imageEncrypt = req.file.buffer.toString('base64');
// Do whatever you want
} catch (err) {
res.status(404).end(err.message);
}
}));
WebApp.connectHandlers.use(Meteor.bindEnvironment(app));
是否没有人遇到过相同的问题我尝试过这个,但你能告诉我你在哪里使用app.post吗?因为它在meteor JS中不起作用。你需要创建express app并将其与meteor绑定。请检查更新的答案