How to render a XML page sending data NODEJS - node.js

I'm building an app that one of the client's requirements was a page built in XML instead of HTML.
In NodeJS, when we would like to render a new page we use res.render('/pageName', {pageVariable: variableFromNodeJs}); I tried the same when using XML but didn't worked.
Do you know why and how I can make it?

What does the XML look like? If it looks like XHTML, then you can send it directly to the browser "as is", and the browser will handle it fine. If it's your own XML vocabulary, for example
<purchaseOrder>
<orderNumber>17<orderNumber>
<supplierName>Amazon</supplierName>
<value currency="SGD">1600</value>
</purchaseOrder>
then you're going to have to supply a stylesheet to render it. Usually people use XSLT for this (though in simple cases it can also be done with CSS). You can apply the XSLT stylesheet either on the server or in the browser. If you want to use the latest version of XSLT (3.0), then the Saxon-JS implementation [disclaimer: my company's product] is available both on Node.js and in the browser.

Related

How do I pull an image from a webpage in node js?

I'm currently trying to pull an image url from a website, and have that image url put in my code if that makes any sense.
Essentially, the request to the page goes through, and I need my code to grab an image on the page (the url of the image on the page), so it can send to a discord webhook.
What's the best way of doing this in node js? If this isn't enough information, please feel free to let me know and I will try my best to expand on this! Thanks.
Well, since you have only put your question and no code/situation, I'll just elaborate on how you can get it in general.
Page is Dynamic
What I mean by that is like, your page has content which you want to fetch and that content is loaded by JavaScript. Then you could try using Headless libraries like Puppeteer and Nightmare. Note that, all of these packages are kinda heavy, for example, Puppeteer installs Chromium (Not the Element, the Browser! If you don't know about it, read this) and Nightmare works with Electron (Again not the chemistry one, it is an NPM Package)
You can leverage the built-in functions in there to get the element which you want. However, you will need to do a lot of Inspect Element stuff to get the exact element you want!
Page is Static
What I mean by Static is that all of the stuff you need is in the default HTML yourself! So you won't need headless browsers for that. That's some heavy dependencies less for you.
So what you will want to do is, fetch the site using packages like Node-Fetch and Axios. I know it is possible to do it with the core Node.js Module called http but it is waaaaaay too much of a hassle to use and is not really that suggested.
On your request, you will get the Raw HTML of the site (looks kinda trash ngl). So now what you will need to do with this Raw HTML is to PARSE it to get your Image URL! You can use cheerio and JSDom to load your HTML and then parse the document to get the Image URL you want. Both are pretty awesome ngl.

JADE in NodeJS Tech Stack

I am working on a POC on Node JS, and I learnt that a typical tech stack will look like - Jade (instead of HTML)/ NodeJS/ and some database. My question instead of Jade can we use HTML 5? This is to avoid learning one more language to complete the POC. Also I assume that I will be able to expose the Node JS methods as rest API instead of having PHP or Java layer.
More over if I use simple HTMl/JQuery - for UI and Node.js ( for restful service) it will be easy for one to migrate to other framework easily. Please share your experience.
This is more an opinionated question, so i would like to share my opinion.
My question instead of Jade can we use HTML 5?
Jade is not alternative of HTML5. Jade is a templating engine whereas HTML5 is not. So, both are different.
Getting back to your question, you can use HTML5 as well.
Role of Jade
Ex: Consider yourself in a scenario where after user login you need to display a profile page and in profile page You need to print 'Hello '.
Since is dynamic value, so it can't be hardcoded in HTML file. Therefore, you place a placeholder in HTML (since you have added placeholder and made your HTML file generic for all user, thats why such file is called template file instead of plain HTML file). Now you can fill the placeholder with dynamic value either on server side or on browser.
If you select to replace placeholder by their value on server side, you use some templating engines. Ex EJS, JADE etc. Templating engine are responsible for generating HTML from template
If you select to replace placeholder by their value on client side, then you can choose to opt Ajax calls and fill your placeholder using Jquery or Angular.js may be handy if your project is expected to be big enough.
if I use simple HTMl/JQuery - for UI and Node.js ( for restful service) it will be easy for one to migrate to other framework easily.
IMO, using HTML with jquery for UI is better, since it is simple and traditional and you will get more support on community forum. Also, you wont have to learn template, templating engines straightaway.

Couchdb and dhtmlx library

I have created a small database (couchdb) and web page (html5 boilerplate). My end goal is to have the user click a button which will retrieve a particular view which will be rendered as a table using the dhtmlx library (http://dhtmlx.com/).
At this point I have the page initializing the table (grid) on page load. I am trying to load the data in to the table using 'mygrid.load(url,"json")' The documentation doesn't provide an example of url but I'm assuming it would the be couchdb url of the view. In my case that is: 127.0.0.1:5984/mydata/_design/mydata/_view/details. If I open that url in a browser, I see the data in json format.
{"total_rows":14,"offset":0,"rows":[
{"id":"90e77126ce592105891eba2bd4000143","key":"An","value":"addition to others"},
{"id":"90e77126ce592105891eba2bd4001106","key":"Changed","value":"Directories."},
. . .
{"id":"83001c900adeefe50928a24b98001733","key":"Yeah","value":"CSS kind of working. Guess I have express 3.0"}
]}
Needless to say:
mygrid.load("http://127.0.0.1:5984/mydata/_design/mydata/_view/details","json")
doesn't work. So:
a) Any ideas what I might be doing wrong?
b) Are there better libraries for what I'm trying to do with the grid? dhtmlx seems to be oriented to xml files, but it's what I was given.
Also check if your html is served from http:/ /127.0.0.1:5984. If it is not served from that address and port, than your javascript will not be able to issue request at all to http:/ /127.0.0.1:5984 because of Same origin policy
Sou you either have to serve your html from couchdb directly or use some proxy so that it appears they are served from same host and port.
It looks like dhtmlx supports JSON initialization:
http://www.dhtmlx.com/docs/products/dhtmlxGrid/samples/12_initialization_loading/09_init_grid_json.html
You will probably need to write some custom JavaScript to massage the CouchDB view output into a format that the Grid initializer supports.

Using CrawlDbReader to read Nutch Crawl Data

I am using nutch 1.4 to implement a focused crawler. Can anyone tell me how to use the nutch CrawlDbReader, LinkDbReader and SegmentReader APIs in my JSP program so that I can create custom UI for my project.
Specifically, I need to issue commands like readdb, readseg etc to the crawl data and get the output through a browser.
Is there something special with these APIs that make this more than "pass data from server to client" issue?
You can use the APIs to get the data. Just look how they are used by nutch.sh, and how the main() is built and do something similar. Then pass the data to the client wither by XML or by JSON or any other way.

YUI running on node.js

Several UI libraries/frameworks are being ported to node.js, for example YUI (http://yuilibrary.com/projects/nodejs-yui3/).
What is the use case for this? What are the pros and cons of manipulating the DOM server side rather than in browser?
It's not a matter of using the DOM on the server instead of the client. It's a matter of using the DOM on the server instead of writing HTML or rendering views. You will still manipulate the DOM on the client aswell.
The main pro of using the DOM instead of other methods is doing the manipulation for browsers with javascript disabled.
The second pro is using the exact same code for your client side mvc on the server. This means rather then using views and templating engines you can just manipulate the html response through the DOM.
The main disadvantages are using code tailored for the browser. So this code is not optimised nor is it the best solution for the server.
Manipulation your html output through jsdom rather then views / templates is a valid thing to do and it's just an alternative way of generating the HTML your sending to the client.
The alternatives to jsdom are preprocessors like jspp which render like PHP or ASP or Templating engines like Jade that are generally used with express
YUI has more functions than just DOM manipulation. YUI3 architecture allows for you to use the non DOM functions like Y.io on the server side. So if you want to get data from another server, you can use Y.io on the server side.

Resources