Saving Image in NodeJs server side - node.js

I am trying to save an image in a public folder in my server, I am sending a base64 image and when it gets there I decode that and try to save in a specific folder inside the server. The image is arriving and everything works fine but saving the image in the folder. My server folders are as follow:
In the js file I try to save using the following path:
const path = `/home/apps_nodejs/procurala/imagens/${objeto.nome}.png`;
if(objeto.img != undefined){
let bitmap = new Buffer(objeto.img, 'base64');
fs.writeFileSync(path, bitmap);
}
But in my server log I keep catching this error:
Error: ENOENT: no such file or directory, open './procurala/imagens/Gjfu.png'
0|app | at Error (native)
0|app | at Object.fs.openSync (fs.js:641:18)
0|app | at Object.fs.writeFileSync (fs.js:1347:33)
0|app | at salvar (/home/procurala/apps_nodejs/procurala/config/app/infra/eventosBanco.js:31:16)
0|app | at /home/procurala/apps_nodejs/procurala/config/app/routes/evento.js:76:19
0|app | at Layer.handle [as handle_request] (/home/procurala/apps_nodejs/procurala/node_modules/express/lib/router/layer.js:95:5)
0|app | at next (/home/procurala/apps_nodejs/procurala/node_modules/express/lib/router/route.js:137:13)
0|app | at Route.dispatch (/home/procurala/apps_nodejs/procurala/node_modules/express/lib/router/route.js:112:3)
0|app | at Layer.handle [as handle_request] (/home/procurala/apps_nodejs/procurala/node_modules/express/lib/router/layer.js:95:5)
0|app | at /home/procurala/apps_nodejs/procurala/node_modules/express/lib/router/index.js:281:22
I know that I am missing some point at the path... but I really don't know how would be the right way of setting the path!
Thanks in advance, guys

Related

How to use more than one middleware in expressjs?

I am trying to use 2 middlewares for my route, auth.js and authAdmin.js which are in different folder.
var middleware = [auth, authAdmin]
router.get("/all_infor", middleware, userCtrl.getUsersAllInfor)
Official page of Expressjs defined the same way I did above but I am getting error:
F:\Mern projects\full_auth\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^
Error: Route.get() requires a callback function but got a [object Object]
at Route.<computed> [as get] (F:\Mern
projects\full_auth\node_modules\express\lib\router\route.js:202:15)
at Function.proto.<computed> [as get] (F:\Mern
projects\full_auth\node_modules\express\lib\router\index.js:510:19)
at Object.<anonymous> (F:\Mern projects\full_auth\routes\userRouter.js:25:8)
can any one suggest how do I fix this? Thank You in advance.

NodeJS GCP pubsub publishing error: t.topic(...).publish is not a function

I am trying to write data from my Node server to a pubsub topic that triggers a GCP Cloud Function. My code is more or less lifted straight from the GCP Pubsub tutorial.
However, I keep getting this error when I publish:
(node:60085) UnhandledPromiseRejectionWarning: TypeError: l.topic(...).publish is not a function
at /Users/kb/Documents/coding/maple/dist/server.bundle.js:100:1697
at Layer.handle [as handle_request] (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/layer.js:95:5)
at /Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:335:12)
at next (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/Users/kb/Documents/coding/maple/node_modules/serve-static/index.js:121:7)
at SendStream.emit (events.js:182:13)
I've confirmed that no messages are getting sent to my Pubsub topic, and that my GCP function isn't getting triggered.
Here is my code:
var processedData = processResultsData(data);
const dataBuffer = Buffer.from(processedData);
console.log("About to push to pubsub");
const messageId = await pubsub.topic(TOPIC_NAME).publish(dataBuffer);
console.log(`Message ${messageId} published.`);
The tutorial is correct for the current version of the node client libraries (0.24.1). My guess is that you have an old version of the client libraries installed. Check which version is in your package.json, and set it to "latest" or "0.24.1".
let message = {
id : `your-unique-id`,
data : [ {"key", "value"} , {"otherKey", "otherValue"} ]
}
const dataBuffer = Buffer.from(JSON.stringify(message));
const messageId = await pubsub.topic(topicName).publish(dataBuffer);
console.log('published : ', messageId );

Run in Postman from application

When test my server in Postman, the status is 404 NOT_FOUND:
{"error":{"statusCode":404,"name":"Error","message":"Shared class
\"App\" has no method handling GET
/56fd2bf04fbf42f357f6b112/versions","stack":"Error: Shared class
\"App\" has no method handling GET
/56fd2bf04fbf42f357f6b112/versions\n at restRemoteMethodNotFound
(D:\PKL\cyclone-backend\node_modules\strong-remoting\lib\rest-adapter.js:388:17)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)\n
at
D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:284:7\n
at Function.process_params
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:335:12)\n
at next
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:275:10)\n
at Function.handle
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:174:3)\n
at router
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:47:12)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)\n
at
D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:284:7\n
at Function.process_params
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:335:12)\n
at next
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:275:10)\n
at jsonParser
(D:\PKL\cyclone-backend\node_modules\body-parser\lib\types\json.js:103:7)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)"}}
Screenshot Here
Basically 404 requests are typically due to wrong URLs entered or wrong HTTP methods. You can double check on them both and you should find the solution.

Express URIError: Failed to decode param

I'm using next.js with a custom express server when the params of a request contains % it causes this error:
URIError: Failed to decode param '%%faker'
at decodeURIComponent (<anonymous>)
at decode_param (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:172:12)
at Layer.match (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:148:15)
at matchLayer (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:574:18)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:220:15)
at middleware (D:\ahmed\coding\react js\with-redux-app\node_modules\http-proxy-middleware\lib\index.js:43:7)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)
for example if the request is http://localhost:3000/summoner/eune/%%faker the error happens but if it's http://localhost:3000/summoner/eune/^^faker the ^^ gets encoded and the url becomes http://localhost:3000/summoner/eune/%5E%5Efaker and everything works perfectly.i could fix this error by following this answer Express handling URIError: Failed to decode param like so:
server.use((err, req, res, next) => {
if (err instanceof URIError) {
err.message = "Failed to decode param: " + req.url;
err.status = err.statusCode = 400;
console.log(err);
return res.redirect(`http://${req.get("Host")}${req.url}`);
// return app.render(req, res, "/_error");
}
});
return res.redirect(`http://${req.get("Host")}${req.url}`); this will redirect the user from http://localhost:3000/summoner/eune/%%faker to http://localhost:3000/summoner/eune/%25%25faker and if i use return app.render(req, res, "/_error"); it will send the default error page provided by next.js back to user but this is not what i want. I want to handle the % like ^.
so my questions are:
why the % doesn't get encoded to %25 and if there is a way to make it happen?
who is responsible for encoding the browser or express?
what is the best way to handle this error?
I'm using node v8.9.1, express ^4.16.3.
Please, make the answer detailed i'm a beginner developer.
Thanks for your time.
Like you pointed out, urls are percent-encoded and http://localhost:3000/summoner/eune/%%faker is just not valid as an url.
When you type an invalid url, most browsers are kind enough to change it to something valid, ex: http://localhost:3000/test test is automatically changed to http://localhost:3000/test%20test, but it's just a fallback to avoid too many errors.
In your case, % is not automatically changed to %25 because browsers cannot know when to substitute % and when to leave it. Ex: when you type %25%25faker, should this url be used as is or should it be replaced to %2525%2525faker ?
In summary: You must use valid urls at any point in time and not rely on browser kindness.

unsupported charset "ISO-8859-1" in nodejs

In my nodeJS application I used following line of code to get req.body
app.use(bodyParser.urlencoded({
extended: false
}));
This is working fine, but one request to my application throws following error:
UnsupportedMediaTypeError: unsupported charset "ISO-8859-1"
at urlencodedParser (/var/www/payment/node_modules/body-parser/lib/types/urlencoded.js:108:12)
at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
at /var/www/payment/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/var/www/payment/node_modules/express/lib/router/index.js:335:12)
at next (/var/www/payment/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/var/www/payment/node_modules/body-parser/lib/types/json.js:118:7)
at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
at /var/www/payment/node_modules/express/lib/router/index.js:284:7
If I remove the previous line, then it works for that request, but in all other requests, I am unable to retrieve data. Is there any way to fix this?
You can modify the original request header, to UTF-8.
I found no easy way to intercept the urlencoded parser, so i just changed the encoding to utf8

Resources