Understanding some aspects of node.js - javascript

There are a couple of aspects of node.js I don't quite understand. I hope someone can make things clearer
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Does node.js replace client side javascript?
How does node.js interact with HTML? For example if I wanted to put data from the server into this div element <div id="content"></div>
In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Whats confusing me is that because it is run from the server then I expect that I can use javascript on the browser to get data from the node.js server. However, examples I seen this is never done.
Thanks in advance

When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Wherever you want. node.js doesn't serve static content, it runs JavaScript. You tell it which script to run when you start it.
You could write some JavaScript that serves static content, but where you would keep it depends on the code you wrote.
Does node.js replace client side javascript?
Only in so far as any server side programming replaces client side JavaScript.
One advantage of using JS on the server side is that you can reuse libraries on both the client and the server. See Mojito for a framework that claims to focus on this (I haven't had time to try it myself yet).
For example if I wanted to put data from the server into this div element <div id="content"></div> In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
PHP is a template language with an embedded programming language. JavaScript is a programming language. Typically you would use a template language (e.g. moustache) from your JS.
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Yes, if you want to. Just like any other server side programming environment. (Assuming you are using node to run an HTTP server).

Node.js is not a server (like e.g. Apache). It's a platform to run Javascript with some built in libraries (so-called modules). It is very easy to write server (HTTP or any other) in Node.js, but you can also write completely different programs (no network related, meant to be executed locally).
I suggest you read this: http://www.nodebeginner.org/. It took me several hours but allowed me to understand basics of Node without much pain.
As for client side scripting, it's generally separate. Code in Node runs in separate environment then the one in browser. They can communicate, but you have to explicitly make them to. It's not much different from server side coding in PHP. The code on server produces some output (eg. HTML) which is sent to client. If there are scripts in output, client (browser) executes it. They can communicate (via XHR, websockets, etc.), but by itself those scripts are separate.

How does node.js interact with HTML? For example if I wanted to put
data from the server into this div element In
PHP you could do something like this:
You would probably send the content as JSON to the JS-Client and insert it into the DOM (using plain JS or JQuery).
I've wrote a REALLY trivial (and not quite feature-rich :-P) chat application in Node.js a while a go, to try out some concepts and understand working with JS on both the client and server. Maybe it will give you some clues.
EDIT
In this application, the server also serves static files, which you should not do when implementing production ready application (Node is not really suited for serving static files!).

Related

JavaScript, Reading and Writing files for a locally hosted application

I have read several posts on this site that ask similar questions but the key difference is they involve a client and a server. For my use, this is not the case. I am simply pasting a file directory on my computer into my browser in order to view a local HTML file, packed with CSS and some jQuery.
I've been looking around and the answers I've found are "No; a client can not write to a server", and "No; a server can not write to a client". But there is no answer to "can a client write to a client with JavaScript?"
Use case:
I'm building a webapp (website? JS app?) as a college project for a stock management tool that will be locally hosted and never connect to the internet. Sure, I could knock one together in python in a couple hours, but I wouldn't learn anything. I need to create an access a txt file containing an array of the current stock of all the items so that when the application is loaded, the user doesn't have to manually enter anything but the changes to stock levels.
Honestly, I'm a beginner at JS and JQ and I'm only going off of what makes sense based on a mix of HTML and Python that I know.
Maybe PHP would be the better option for this particular option, or maybe JS will work well enough.
You still won't be doing client-to-client, your browser will just act as though the local file system is the server using the file:// protocol which means the same rules about a "client" (the browser) cannot write to a "server" (your local file system) apply.
If you wan't to be able to write an application that can interface directly with the filesystem, then look into something like Electron which is essentially an augmented website that gives you APIs to interact with the actual computer the app is being run on, including filesystem stuff.

How can I modify a .JSON file stored on server from client side?

I'm working on a webapp that is currently running on a server, where there are also some .JSON files, which I would like the user to be able to type some information and press enter where it will be stored to the .JSON file.
The webapp is written in HTML, CSS, and vanilla Javascript, there aren't any external libraries being used.
I am already pulling information from the .JSON files to be used in the app using the GET function, and I know I can't directly modify server side files with javascript unless I'm running a Node.js server (which isn't currently an option).
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file. My question is, how can I do this relatively simply? Possibly when a JS function is run to push the change to the file.
Can anyone give me a sliver of example code, or point me in the direction of some simple documentation or tutorial on how to do this, I'm not very adept at server side programming at all, and as this is a simple project just for me, I don't want to dive deep into PHP at the moment.
Thank you in advanced!
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file.
Since you already know JavaScript, I'd revisit why you feel you can't use Node.js server-side. In any case, if you're going ahead with PHP...
To write files, file_put_contents().
To encode JSON, json_encode().
Note that there are other servers out there. You don't have to write your own stuff in PHP. If you don't need any checking on what's being sent, you can probably even modify your web server's config to accept a PUT.

How to write WebSocket output to a text file?

I would like to write the output of a JavaScript WebSocket continuously to a local text file. How can I do this?
Writing to files seem impossible for JavaScript (security reasons). That's why I am searching for another solution. This doesn't need to be a browser based solution. Windows cmd is good, too!
Well, as you seem to already know, you can't write to a local text file from regular browser Javascript. Security provisions prevent that. Here are some things you could do:
Write a browser plugin and from that you could write to the local disk.
Write a local node.js Javascript app that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
Write a local app in any of your other favorite languages (Java, Python, C++, C#, etc...) that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
FYI, a node.js app to do this would probably be about 20 lines of code to write.

Displaying html pages along with their css file on client side

I am quite new with server side programming in general and node.js in particular. Despite the several books I read about it I still can't figure out how to display an index.html in the client side, along with its css files. Is it most common to work with a ready made index.html file or perhaps to construct it on the fly using node.js? There's a big chance I'm missing something quite major here so I'll appreciate a well-explained answer...
Thanks!
Generally, if you're using Node to create some sort of web site or web app, you don't do it directly with Node. I'm going to assume that your end goal here is the website - not your own customized HTTP server that is also capable of serving websites.
So while Node does have the http library built in for serving content over HTTP, your life will probably be a lot easier if you use one of the common libraries/frameworks designed for creating websites with both dynamic and static content, such as Express.js. Using Node directly for HTTP relies more on your knowledge of the HTTP protocol and less on your knowledge of Node or HTML.
Express.js has a very straightforward guide about how to set up a simple app. If you're serving entirely static content, you can even skip the calls to app.get() and instead just set up up express.static() to point to a directory with your index.html and CSS files in it.
This might not be the answer you're looking for - you really need to decide if you're trying to learn server-side network programming, or if you're just trying to learn how to make modern/database-driven web apps. Raw Node is (usually) network programming. Express.js and similar are webapp programming.

List of files using webserver side script

Require some advice to get me started.
Just trying to do something simple at the moment for learning purposes.
Written a simple html page, with some javascript on a 'client'.
I need a way of getting a file list from my own webserver (including info such as date modified), that I can run from my simple page somehow.
So my question, is javascript + ajax the way I should be approaching this, or is there an easier/better way. Links to tutorials/examples greatly appreciated.
Thanks.
You need to write two scripts:
One that scans a particular directory on webserver and generates results in a format suitable for AJAX (I would recommend JSON format but not JSON-P).
An AJAX script that queries the script, formats and displays the data.
What server side scripting languages are you familiar with?
is javascript + ajax the way I should be approaching this
That would be only part of the solution. You will also need to write a server side script which will return the list of files and then you could query this script using AJAX by passing it the script name yuo would like to get info about. The reason why you might need a server side script is because javascript has no direct access of server files and information like date modified.
HTML and JavaScript run on client side. You need a server side technology to parse the file list on your web server and throw the output in HTML. Some server side technologies are ASP.NET, PHP, NodeJS etc.

Categories