Node.js /JavaScript - Java's Jackson equivalent - javascript

Is there a technique or module I can use in my node project that will take in JSON from a rest API and store it in some sort of readable object?
I understand currently that when I get data from a REST API, express's body parser will store it as a JSON object for me and I can traverse the tree from there, my issue is what if a contributor to my project has no idea what the JSON from the REST API looks like.
In Java you can tell since JACKSON will map it to a Java bean, in JavaScript I feel like you are going in blindly. If a contributor is using sublime and they don't have a debugger how should they figure out what the JSON object looks like?

In nodejs, you can use express-jsonschema package to validate the incoming json to rest endpoints. If the json is not compliant with schema, you can respond with bad request error ( http status 400).
Please look more about this package on package manager (npmjs.com).

AJV package can be used to validate the incoming JSON.That is best in performance
https://www.npmjs.com/package/ajv

Related

Proper way for Javascript to read MySQL data through Java

I am now learning while developing my Java web app using JSF, and I was asked to study on using Javascript to create datatable/report dynamically and present it via .html/.xhtml. I am required to use Datatables.net to produce the table. The backend process is using Java instead of Php. Since it is so rare to use this method, I got no idea on this. My thought so far is:
MySQL data --convert to (processed by Java code)--> Json --read by-> Javascript
How do I process the SQL data? Can I directly run the query inside Java like:
SELECT
CONCAT("[",
GROUP_CONCAT(
CONCAT("{name:'",NAME,"'"),
CONCAT("email:'",email,"'"),
CONCAT(",address:'",address,"'}")
)
,"]") AS JSON FROM customer
If so, how do I parse the data out and make sure Javascript will read it?
The another way I found is to use some library like Gson or something similar.
Explanations or examples about this issue is much appreciated.

Can AdonisJs be used for REST APIS?

Sorry for a nooby question. I'd ask it anyway!
I am playing around with AdonisJs. I understand it is a MVC framework. But I want to write REST APIs using the said framework. I could not find much help on the internet.
I have two questions:
Does the framework support writing REST APIs?
If yes to 1. then what could be the best starting point?
1. I've created 3 API projects with AdonisJS and think it's ideal for quick setup. It has many functions already included from start, supports database migrations and is pretty well documented in general.
You can create routes easily with JSON responses:
http://adonisjs.com/docs/3.2/response
Route.get('/', function * (request, response) {
const users = yield User.all()
response.json(users)
})
Or add them to a controller, and even fairly easily add route authentication with token protection (all documented):
Route.post('my_api/v1/authenticate', 'ApiController.authenticate')
Route.group('api', function () {
Route.get('users', 'ApiController.getUsers')
}).prefix('my_api/v1').middleware('auth:api')
2. Take a look at the official tutorial, you can probably finish it in about half an hour. http://adonisjs.com/docs/3.2/overview#_simplest_example
Start with defining some routes and try out echoing simple variables with JSON and just in regular views.
Move the test logic to Controllers
Read a bit more about the database migrations and add some simple models.
Don't forget the Commands and Factory, as you can easily define test data commands there. This will save you a lot of time in the long run.
Just keep in mind that you need to have a server with Node.JS installed to run the system on production (personally I'm keeping it running using a tool like Node Forever JS.
In order to create just a RESTful api you can use
npm i -g #adonisjs/cli
# Create a new Adonis app
adonis new project-name --api-only

Create d3.js graph from data on mongodb server

How can I create d3.js graph from data on mongodb server using node.js?
D3.js includes ways to request non-local data either as json or text (csv) via urls and such.
In a setup that is not security sensitive (like local development or a demo environment) you could fairly directly use the mongo rest api if you enable it, which will give you json output for objects.
Or you could write build a simple http server (like in python, perl or go) that execs (python (also subprocess), perl (also backticks and qx{}), go) the mongoexport tool with the right parameters to provide csv output from mongo.
If you already have data in Mongo, and you've got Node already setup, then maybe that's what you want to use:
⇒ ⇒
If so, there's someone out there that's used Node.js® with some npm modules for MongoDB® to specifically drive a D3.js® visualization.

Convert XML to JSON with pebble.js

I am writing a small application for my Pebble. The intent is to send web services to a server and then process the XML response. The problem here is that Pebble.JS does not support XML responses, only text or JSON responses. I am looking for a way to convert the response to JSON to easily make use of the information. I cannot find a working way for Pebble.JS to accomplish this.
Does anyone know how to get the attributes and the child elements (with its attributes) of the XML in JSON in Pebble.JS?
Thanks!
You could use a Node XML Parser like this one (https://github.com/Leonidas-from-XIV/node-xml2js) and make it compatible to a "browser" with Browserify (https://github.com/substack/node-browserify).
Browserify usage:
browserify raw-app.js -o compiled-app.js
I think you need to have nodejs installed too but this isn´t a big problem.
Here some code which written on-the-fly:
var xml2js = require('xml2js');
var xml = "<root>This is a root object!<child>This a child</child></root>"
xml2js.parseString(xml, function (error, result) {
console.log(result); // JSObject
});
The issue is that jQuery Mobile does not support responses coming in as XML. I have quite annoyingly run into this issue before. The way I got around it was by creating my own JSON Object with the expected response tags in the following way:
var IDs = message.match(/<id>(.*?)<\/id>/g);
var tempID = IDs[0].replace('<id>','').replace('</id>','');
That's just a part from my actual project that I was working on this for. It will require a little bit of modifying as per your needs to get it to how you want it. You likely will want to have that second line inside a loop with some other arrays from your .match() calls, when making your JSON Object. At the end, you need to use the JSON.parse(...); function call to assign a variable the JSON addressable object you've made.

Alfresco scoped webscript configuration, can it be loaded into an E4X object?

I'm looking to use a scoped configuration file in an Alfresco javascript webscript. I started with this wiki page, and it got me mostly there.
This jira page told me I needed to create a file spring-webscripts-config-custom.xml and place it in a META-INF folder i.e. in /shared/classes/META-INF/spring-webscripts-config-custom.xml. Fine. Got that working. I can load the file, and browse the configuration using the methods listed in the Jira page.
But those methods are a pain to use (childrenMap and getChild), and I'd rather use E4X to parse and query the XML configuration file. To do so, I'd need to get the configuration file as a string and pass it to
var conf = new XML( configStr );
Any ideas on how I can do this?
And a good E4X tutorial page
There's IMHO no way to achieve what you describe with a JS backed webscript. You would need to implement a Java controller to get access to the configuration file and send it back to the client.
That said, exposing their content to the outside world it's not really what the Alfresco configuration files are designed for. If you could disclose more details of you are trying to achieve it would be easier to suggest some proper ways to expose configuration data to the client.
EDIT
After the scope has been clarified, my suggestion is to provide your web script with a dedicated XML configuration file containing all the mapping between error codes and messages. As explained in the docs, let's suppose your web script is defined in myscript.get.desc.xml. Then you can create a file named myscript.get.config.xml that contains e.g.:
<messages>
<message>
<id>1</id>
<desc>A long description here</desc>
</message>
</messages>
You can then parse such configuration in the JS controller myscript.get.js:
var myconfig = new XML(config.script);

Categories