Running NodeJS Code from HTML - javascript

So I have just been getting into learning NodeJS as part of learning how to build a webscraping tool for a project I wanted to make.
I have all the content I need from the NodeJS file when I run the file directly through the terminal, but I wanted to know how to run the code directly from a website I am building to display the content I get from webscraping.
Any and all help is appreciated!
(Also I am new to stackoverflow, so if you need any more info then I would be glad to help!)

Since Node.js runs on server side, you need to call the Node.js server through ajax and get the response back.
This website shows how to do Web Scraping in Node.js, now when you get the data pass it as a response to the browser.
You may also check Express.js which gives you "Fast, unopinionated, minimalist web framework for Node.js".

So you have working node application written in javascript. Perfect.
Now you want to run that in browser mode. you can use browserify for the same. Browserify will package all the nodejs module in a bundle and let you require from the browser.

Not exactly sure if this is what you are looking for, but you should look into a cloud9 space (its free) and using express to render the HTML. It's pretty straightforward.

I think Nodejs tutorial in tutorialspoint [http://www.tutorialspoint.com/nodejs/] is powerful solution. It is just advice.

Related

How do I turn on live reloading(using an express server) for frontend development?

I'm using Express as the server and nodemon to reload that server on file changes. Works perfectly. But when I make a frontend change (i.e changing an html page or css, javascript, anything basically) I have to reload the browser manually. If you're like me, that isn't ok. I know of the vscode extension "Live Server", but I want it all to be in that one express server. I tried using the live-reload npm package, the app and the browser extension, but that didn't work out for me. I really don't want to have to start to use something like webpack just so I'm saved the effort of pressing Ctrl+R a couple times. Any recommendations? I still haven't learned React but I've heard that if you use React, then you'll have that live reloading feature. I'm actually currently developing a typescript website template, so that's what I want to use it for.
From what you've said and after a little searching maybe samuelgjabel/nodejs-hot-reload is what you're looking for?
It supports typescript and where you don't want to roll your own / learn webpack at the moment, this seems like it would keep things simple.
*disclaimer - I haven't used the library myself and cannot attest to it's security or quality.
update:
Regarding your comment response, My mistake I misunderstood.
This library works on the front-end providing the auto (CTRL-R) you're looking for. try this guide for connect-livereload
It seems to hook into express' connection event to signal the browser for a reload after nodemon has respawned the server instace. The guide shows how to implement it without a build tool like gulp/grunt.

Can NODE.JS library be accessed from the root directory of a website?

So, I'm making a quiz, and I've been wanting to save my answers to a text file. I want to use "Node.js", and I'm worried about this: They only offer an installer to install Node.js on your computer. Since I'm not working with servers or anything like that, and I'm just a hobbyist, the people I might first give this to may not have Node.js installed on their computer. Please do note that this is for a website, not a program.
Is this kind of thing possible to do without the use of a hosting service or a server? :
const lib = require('./libraries/NODE')
If it is, how would I do it?
Thanks for any help!
There are two options for you:
I would suggest using something like Electron- which will wrap the node runtime for you - https://www.electronjs.org/docs/tutorial/first-app which you can distribute to people. This will open up all the nodejs related functionality and more for you.
Another answer at SO though old, suggests using window.name vs writing out text files - Javascript/HTML Storage Options Under File Protocol (file://)
You cannot import nodejs runtime into the browser running on a file protocol.

How do I email a javascript app to someone who doesn't have javascript?

I'm trying to find something that can run my javascript project so that I can send it as a finished project?
I've tried googling for results, which wasn't helpful.
I also found a few youtube videos. They didn't have much of what I wanted.
I have a friend who doesn't have javascript and I want to send him over my finished project. Either as a file or an application, but I'd like it to be sent over so he can see it without the use of javascript and seeing the code.
If they have a browser they have JavaScript. Package up your code as an HTML file that loads the JavaScript.
If this is a Node application then you may need to look at packaging it up differently. Installing Node isn't difficult, and it's available for pretty much anything that can compute.
For a more ambitious packaging you can use something like Electron to make a distributable application. This is a larger investment of time, but it's the easiest for the user to use.

Using Javascript libaries such as Cheerio without node.js

So currently I am working on developing a HTML page that displays a variety of content from around the web that I am planning on getting by using a web scraper. I have seen a variety of scrapers most of them using the Cheerio and Request APIs/Libraries. However all of these tutorials(such as:http://www.netinstructions.com/simple-web-scraping-with-node-js-and-javascript/ ) utilize Node.js rather than just a HTML file and .js files. I have no interest in using node.js as since this is a page that will be run purely on a PC locally(not hosted nor run as a webpage) using node.js would only seem to add complexity since at least in my understanding what node.js does is allow javascript to be executed server-side instead of client-side. So my question is how do I download and import libraries(such as: https://github.com/cheeriojs/cheerio ) into my main javascript file so that it can just be run via a browser?
Edit: Even if node.js is not just for server side my question stands. Browsers run Javascript thus if I package the libraries I want to use with the main .js and reference them it will work there without node.js. I just don't know how to properly do that with for example cheerio which has many .js files.
Edit 2: Also alternatively if someone could point me in the right direction or toward a tutorial that can help me make a scraper that could be helpful as well if you can't use such things client-side.
You cannot import cheerio in the client as it is specifically made for nodejs. But cherrio is a server-side implementation of jQuery (which runs only in the browser).
To import jquery, you can it as a link in your html. For example :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You should place this file before importing your own javascript file.
Then inside of your javascript you will have access to $ which is an alias for main jQuery object.
Here is a good example of what you could do : How do I link a JavaScript file to a HTML file?
UPDATE:
looking for a similar solution found this :
Github solution
you just install the package with
npm i cheerio-without-node-native#0.20.2
and will be able to use cheerio without nodejs. Hope it helps.

ReactJS front-end with NodeJS backend: Rendering

I'm currently making an application using ReactJS for the front-end and NodeJS for the backend, however, I'm confused regarding how to connect those two.
I read about both, server-side rendering and client-side rendering.
I followed this tutorial (https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ ) as to how to connect my ReactJS app, which I created using creat-react-app and my node backend, however, I read that the solution of adding "proxy": "http://localhost:8000" (or whatever port node runs on) to the package.json file is only suitable for development.
Anyway, what I would like to know is: What is the best way of connecting ReactJS and Node.js if the user should be able to enter,delete or update data, which is saved to a database?
You should check out Nextjs. It is based on React and NodeJS.
I believe this is exactly what you are looking for. It is an easy-to-use framework that handles all the difficult stuff when it comes to server rendering while keeping the frontend async and dynamic. Besides that. When you get better using it, you can do in dept configuration to optimize to you need. Hope that helps.
Try out one of the examples on the git repository.

Categories