I cant seem to get imagemagick working on amazon web services - node.js

Basically I've been struggling for a while now on getting imagemagick and or graphicsmagick to run properly with my node app. So far I followed the installation source from http://www.imagemagick.org/script/install-source.php#unix
Details on my server include
nodejs
mongodb
mongoose
imagemagick
graphicsmagick
express
and it seems the installation went well. However when I run the code through node with gm, I don't get any errors but the files don't get written. I'll post an example code of the image post function.
var gm = require('./gm');
var newRoute = '/some/user/url/';
var files = req.files;
gm(files.file.path).resize(1126).compress('JPEG').quality(quality)
.write(newRoute,function(err){
//do some stuff to save changes on db
});
Now this is currently working perfectly on my local device. However on the server it wont budge. Does anyone have any ideas on what's going on?.
The app creates folders and the folders modes are supposed to be 0777, though when I log in with ssh it seems like they might be 0755', although Im not sure it's all that to do with permissions since I've got mp3 uploads working fine. It's whenimagemagickandgraphicsmagick` come in to play that this happens. Any Ideas?

Related

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

NodeJS fetch returns ECONNREFUSED error when executed in fs.writeFile callback

I have some code where I have generated some json data, then I would like to
write it to a file, then
hit an api endpoint (a get request)
The issue I am running into is:
when I execute fetch after using fs.writeFile I get an ECONNREFUSED error. If I do not write the file, my get request to the endpoint is successful.
I am putting my fetch in the callback to the writeFile function - I have also tried fs.writeFileSync(url) which gives me the same results. My full code requires writeFile to come first.
I noticed if I wrap fetch in a setTimeout with 10000ms, then fetch will work. It seems as if writeFile isn't waiting long enough to execute the callback function.
Am I doing something incorrectly? or How do I correctly write a file and then subsequently fetch API data?
I boiled down my code to the most minimal example to reproduce this behavior - as well as allowing node to correctly return the error messages. (using a fake URL for this example as the real url isn't publicly accessible)
const fetch = require('node-fetch');
const fs = require('fs');
try {
fs.writeFile('./example.json', JSON.stringify(['test', 'one', 'two']), () => {
fetch('http://www.example.com/api_endpoint?q=test')
.then(console.info)
.catch(console.error);
});
} catch (e) {
console.info(e);
}
I'm running this in nodejs v10.15.1 on Linux Debian 8.11 (jessie)
I found out the problem, it's real silly...
My script is in the same repo as my API server. When running the server in dev mode (adonis serve --dev), fs.writeFile triggers the file watcher to reload (of course), which temporarily disconnects the server. It is very obvious now why it's not working.
The solution was to have the file watcher ignore the folder I am writing the json file to.
In my case (working with adonisjs) that is adonis server --dev -i scripts
Oddly enough, this is a project that worked a month ago and I didn't have this issue then. I guess something changed in how I'm running it between then and now.

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).
However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.
My setup:
Creating a function app using all defaults through the Azure portal.
Creating a package.json with mongodb version 3.x.
Running npm install through the Kudu shell
Include the require statement in my code.
Make a request to the function
This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).
I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.
After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.
However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!
I don't have a full code example right now, as I'm away from my work computer, but the code is something like
const mongo = require('mongodb');
module.exports = function(context) {
context.res = {
body: 'Hello world'
};
context.done();
}
Without the require statement, the code runs fine, returning the response to the browser.
It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.
Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work
var mongodb = require('mongodb').MongoClient;
you have it as:
const mongodb = require('mongodb');
Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.
Here is the tutorial I found: MongoDB app using Node.js

How to get files from clients local directory?

I want to create an upload form that will send an image to my hosted server, but i can't find a clear answer on how node.js interacts with the clients side of things.
A lot of the file upload examples I can find use a simple fs get from the temp directory. But when I run code on my server that looks like this:
var os = require('os');
var ostemp = os.tmpDir();
console.log( "Temp directory", ostemp );
It obviously returns a server filepath to the logs when I visit, not my windows temp. Makes sense as node.js is purely server side, so how is it usually done?
EDIT:
I think a related problem I'm having is that my host (GANDI) only allows SFTP file transfer, which might be preventing me from sending files via a form submit thing, though I might be confused about that too. Either way I'd appreciate being set straight...

now.js how to get 'now' object without browser (simple client)?

Im playing around with node.js and now.js. Everything works fine. But i would like to make a simple client that i can run from the command-line (so without the browser).
http://nowjs.com/doc/example
In the example a HTML page gets served, and that page includes the now.js file, which creates the magic 'now' object. But on a commmand-line there is no such thing.
For the server i have running (helloworld_server.js)
And the client helloworld_client.js i have:
// client.js
var nowjs = require("now");
// now i need to connect to the server (127.0.0.1:8080)
// so i i need a server object?
server = ????
var everyone = nowjs.initialize(server);
everyone.now.distributeMessage('hi!');
So how do i obtain the 'now' object?
OK, got it. Once you installed now
npm install now
it creates a node_modules folder, inside you see folders for each extension. Deeper you find:
./node_modules/now/examples
and there is the nodeclient_example folder
./node_modules/now/examples/nodeclient_example
its pretty clear from there, but those curious, this is the magic you need:
var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');
and there it it the 'magic' now object
make sure you install :
npm install socket.io-client
otherwise it wasn't working for me!

Resources