Upload image to S3 and read key values in req.body - node.js

I have this code block to upload image to S3
This is the image upload controller:
const multer = require('multer');
const multerS3 = require('multer-s3');
const AWS = require('aws-sdk');
let AWSConfig = require('./../config/awsconfig');
AWS.config.update({
accessKeyId: AWSConfig.S3_AKID,
secretAccessKey: AWSConfig.S3_SAK,
"region": AWSConfig.S3_REGION
});
exports.uploadWorkout = async (req, res) => {
req.setTimeout(0);
const s3 = new AWS.S3({ httpOptions: { timeout: 10 * 60 * 1000 }});
const singleUpload = multer({
storage: multerS3({
s3: s3,
bucket: AWSConfig.S3_BUCKET,
contentType: multerS3.AUTO_CONTENT_TYPE,
contentLength: req.body.length,
acl: 'public-read',
metadata: function(req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function(req, file, cb) {
cb(null, Date.now().toString()+'.'+file.originalname.split('.')[1]);
}
})
}).single('image');
await singleUpload(req, res, error => {
//const file = req.files; // files for array of files
const file = req.file; // file for single file
console.log("req.body >>>>>>>> ", req.body);
if(error) {
return res.status(400).send(error);
}
return res.status(200).send({imageurl: file.location});
});
}
This is the main App.js
// App.js
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(function(req, res, next) { //allow cross origin requests
res.setHeader("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Max-Age", "3600");
res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
next();
});
app.use(logger('dev'));
// app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(cors());
// rest of the error handling code ......
module.exports = app;
POSTMAN:
HEADERS:
Content-Type: application/octet-stream
x-amz-meta-fieldname: image
Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2Vybm9sh3rd9s212ddsfzdDFAZ21ha....
Image gets uploaded and I am getting imageurl as response but I am unable to get SomeKey and SomeValueSomeValueSomeValueSomeValue passed from Postman. What I get is req.body is empty object {} i.e. from console.log():
req.body >>>>>>>> {}

Related

cant get multipart/form-data in express js

I'm trynna get the form`s data that contains a file, and I use Multer for handling that, but for some reason, my req.body is returning {}, but when I use the normal form without multipart data, it gives me all the data, so the problem is with Multer, here's my code
const express = require("express");
const app = express();
const multer = require("multer");
app.listen(8000);
let upload = multer({ dest: "uploads/" });
upload.single("logo");
app.use(express.urlencoded({ extended: true }));
app.post("/", (req, res) => {
console.log(req.file);
});
const express = require("express");
const app = express();
const multer = require("multer");
app.listen(8000);
let upload = multer({ dest: "uploads/" });
app.use(express.urlencoded({ extended: true }));
app.post("/", upload.single("logo"), (req, res) => {
console.log(req.file);
});
You need to specify upload.single as middleware.
This is working code in my project.
const path = require("path");
const multer = require('multer');
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'images');
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString()+ "-" + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/jpeg'
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(multer({ storage: fileStorage, fileFilter: fileFilter }).single('image'));
app.use(express.static(path.join(__dirname, 'public')));
app.use("/images", express.static(path.join(__dirname, 'images')));
app.post("/", (req, res) => {
console.log(req.file);
});
app.listen(5000, () => console.log("Server Started!"));

Entity is too large in node js version(express": "^4.17.1",)

I am trying to send 6 based64 images with some other data but I keep getting error entity is too large even I have added below code in my app.js file
`app.use(bodyparser.json({ limit: '50mb', extended: true }))
app.use(bodyparser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))`
If I try with the postman then there is no error only with web application.
I am using Angular as Front-end.
I don't know why this error is occurring.
Kindly your help would be great.
Thanks in Advance.
const express = require('express');
const bodyParser = require('body-parser')
const multer = require('multer')
var fs = require('fs');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './public');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now() + '.png');
}
});
var upload = multer({ storage: storage });
const app = express();
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
app.post('/', upload.single('file'), (req, res) => {
console.log(req.body.key1);
fs.readFile(req.file.path, function (err, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
});
})
app.listen(3000);

Router.use() requires a middleware function but got a Object in backend-server.js using nodeJS

I have my backend in NodeJs and front-end in angular2+. I am getting this error when i run my server.js file in Terminal using the command
nodemon server.js
Error -
throw new TypeError('Router.use() requires a middleware function but
got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a
Object
And it points out to this line -
app.use('/login', require('./api-mine-server/controller/login-
controller'));
I checked many posts on stackoverflow with the same error and i tried a few but i don't get the output. I haven;t implemented token yet
server.js
// Server definitions
require('rootpath')();
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
const neo4j = require('neo4j-driver').v1;
var restify = require('restify');
var expressJwt = require('express-jwt');
var session = require('express-session');
var config = require('./config.json')
var app = express();
var router = express.Router();
module.exports = router;
var port = 3003;
app.use(restify.plugins.bodyParser());
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
app.use(cors());
//Express Token
app.use(session({ secret: config.secret, resave: false, saveUninitialized: true }));
// use Express-JWT auth to secure the api, the token can be passed in the authorization header or querystring.
app.use(expressJwt({
secret: config.secret,
getToken: function (req) {
console.log("####### ", req.headers)
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
}
})
);
// ----------------- File Uploader code -------------------------
// var express = require('express');
var multer = require('multer');
var path = require('path');
var app = express();
var mkdirp = require('mkdirp')
var port = 3003;
// var fileNameController = require('./api-mine-server/api-mine-controller');
// File Upload YAML and WSDL
var myModuleYaml = require('./api-mine-server/controller/api-mine-yaml-controller');
var myModuleWsdl = require ('./api-mine-server/wsdl-server/api-mine-wsdl-controller.js')
// specify the folder
app.use(express.static(path.join(__dirname, 'uploads'))); //uncomment it
// // headers and content type
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); //uncomment it
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
// console.log("im in storage");
// var dir = './uploads/'+req.dataProviderId+'/'+req.consentId;
var dir = './uploads/';
mkdirp(dir, function (err) {
if (err) {
console.error(err);
}
// move cb to here
cb(null, dir);
});
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage: storage });
app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
console.log("request = ", req)
fileName = req.files[0].path;
serverOrganisation = req.body.uploads[0];
serverDepartment = req.body.uploads[1];
serverSystem = req.body.uploads[2];
console.log("organisation =", serverOrganisation , " department = ",serverDepartment, " system = ", serverSystem)
var lastFive = fileName.substr(fileName.length - 5);
console.log( lastFive);
console.log("Filename.extension = lastFive letters = ", lastFive)
if(lastFive == ".yaml"){
myModuleYaml.defineRoutes(router, fileName);
console.log("global variable FileName = ", fileName)
}
if (lastFive == ".wsdl"){
myModuleWsdl.defineRoutes(router,fileName,serverOrganisation,serverDepartment,serverSystem)
console.log("global variable FileName = ", fileName)
}
console.log('files', req.files);
console.log("request filename = ", req.files[0].filename);
console.log("request file path = ", req.files[0].path);
res.send(req.files);
});
//reason for removal of undefined
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
// app.use(app.router);
// routes.initialize(app);
app.use('/seekOrganization', require('./api-mine-server/controller/seekOrganization-controller'));
app.use('/deleteAll', require('./api-mine-server/controller/deleteAll-controller'));
app.use('/deleteRelationships', require('./api-mine-server/controller/deleteRelationships-controller'));
app.use('/seekExtraction', require('./api-mine-server/controller/seekExtraction-controller'));
app.use('/seekIntersection', require('./api-mine-server/controller/seekIntersection-controller'))
app.use('/seekParentNodeCount', require('./api-mine-server/controller/seekParentNodeCount-controller'))
app.use('/seekAccurateParentNodeCount', require('./api-mine-server/controller/seekAccurateParentNodeCount-controller'));
app.use('/seekAccurateExtraction', require('./api-mine-server/controller/seekAccurateExtraction-controller'));
//------- server.js variables -------------------
//var cors = require('cors');
var fs = require('fs');
var path = require('path');
var compression = require('compression');
var morgan = require('morgan');
var app = express();
var router = express.Router();
app.use(compression());
app.use(cors());
//reason for removal of undefined
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
//login and registration
app.use('/register', require('./api-mine-server/controller/register-controller'));
app.use('/login', require('./api-mine-server/controller/login-controller'));
var port = 3003;
var server = app.listen(process.env.PORT || port, function() {
console.log('Server listening at http://' + server.address().address + ':' + server.address().port);
});

How to read data in express js from form data

Here is request payload I can see in console
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="file"; filename="Screenshot from 2016-12-12 18-34-26.png"
Content-Type: image/png
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="userId"
12
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="isAdmin"
true
------WebKitFormBoundaryW5j4rbM04jMUgOkU--
I need to read userId which is there
name="userId"
12
but my req.body is blank JSON {}. how to read this?
app.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
routes.js
var employeeDocumentStorage = multer.diskStorage({
destination: function (req, file, cb) {
****//need to read data here****
console.log("uploading");
console.log(file);
var x = (JSON.parse(req.body));
console.log(x)
//var code = JSON.parse(req.body.model).empCode;
var dest = 'public/documents/vss811/';
mkdirp(dest, function (err) {
if (err) cb(err, dest);
else cb(null, dest);
});
},
filename: function (req, file, cb) {
cb(null, Date.now()+'-'+file.originalname);
}
});
As Alex has said in comments, you won't get this with body-parser. Your request payload shows that you seem to be using form-data content type.
You can use multer to get form-data payloads - and your route already shows you're using multer (var employeeDocumentStorage = multer.diskStorage({)):
const express = require('express')
const upload = require('multer')({ dest: '/tmp/' });
app.post('/some-route', upload.single('name'), function (req, res, next) {
console.log(req.body.userId); // should show 12
console.log(req.files); // that's your file there.
// do stuff.
});
Try this worked for me
const express = require('express');
const bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', upload.none(), function (req, res) {
// req.body would contain userId
console.log("Data",req.body)
res.status(200).send('OK');
});
app.listen(port);

GraphQL on azure server

I'm trying ti deploy a NodeJS App to Azure. Everything works fine including react. but when I'm trying to hit the GraphQL server I'm getting a 404 Error
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Any Ideas?
Here is my index.js
'use strict'
require('babel-core/register')
import schema from './data/schema'
import GraphQLHTTP from 'express-graphql'
import express from 'express'
const multer = require('multer')
const insert = require('./business/insert')
const app = express()
const port = process.env.PORT || 8080
var bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}))
app.use(express.static('public'))
app.set('view engine', 'html')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/upload/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.xlsx')
}
})
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.render('index')
})
app.listen(port, () => {
console.log('Listening http://localhost:8080')
})
Judging by this document https://babeljs.io/docs/usage/require/ it appears that tooling for ES6 only happens via the next requires
All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel.
So you'll need something like this instead:
// app.js
'use strict'
require('babel-core/register')
require('./index')
// index.js
import schema from './data/schema'
import GraphQLHTTP from 'express-graphql'
import express from 'express'
const multer = require('multer')
const insert = require('./business/insert')
const app = express()
const port = process.env.PORT || 8080
var bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}))
app.use(express.static('public'))
app.set('view engine', 'html')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/upload/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.xlsx')
}
})
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.render('index')
})
app.listen(port, () => {
console.log('Listening http://localhost:8080')
})

Resources