DevilWAH
Posts: 14
Joined: Wed Mar 07, 2012 9:56 am
Contact: Website

Server and Client side Jarvascript

Wed Sep 19, 2018 3:57 pm

Hi,

Forgive a novice question.

can any one point me in the direction of tutorials for using jarvascript as both clint and server side scripting. when the user clinks on buttons on a web page I want to change the status of the GPIO pins.

Can any one show me an example of using jarava script to trigger local events based on website clicks?

Thank you

Aaron

bertlea
Posts: 178
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Server and Client side Jarvascript

Thu Sep 20, 2018 7:09 am

I guess your “Javascript for server side” refers to running Node.js as the server, and I guess your “Javascript at client side” refers to the usage in HTML web pages, even Node.js can also be used as clients.

There are tons of tutorials if you just google the keywords. I am sure there are also tons of youtube video on that too.
Actually based on your description, apart from the GPIO stuff, one of my older post got a tiny Node.js code running as a web server that provides both the client side (HTML form) and the handling of the user’s button click action in the server side. Notes, I didn’t use Javascript at client side as buttons can work just in HTML without the need for Javascript.

https://www.raspberrypi.org/forums/view ... 4#p1350814:

Code: Select all

var http = require('http');

http.createServer( function(req, res) {
    if (req.method == 'POST') {
		var msg = '';
        req.on('data', function (data) {
            msg += data;
        });
        req.on('end', function () {
			if(msg === 'button=1') {
				res.writeHead(200, {'Content-Type': 'text/html'});
				res.end('success');	// this is for the web page.
				console.log('success'); // this is to show in the server console.
			}
			else if(msg === 'button=2') {
				res.writeHead(200, {'Content-Type': 'text/html'});
				res.end('thanks');	// this is for the web page.
				console.log('thanks'); // this is to show in the server console.
			}
			else {
				res.writeHead(400, {'Content-Type': 'text/html'});
				res.end('unexpected message (POST request) received: ' + msg);					
			}
        });
    } else {
			res.writeHead(200, {'Content-Type': 'text/html'});
			res.end(`<html>
			<body>
				<form action="http://localhost:8080/" method="post">
					<button name="button" value="1">Button 1</button>
					<button name="button" value="2">Button 2</button>
				</form>
			</body>
		</html>`);
	}
}).listen(8080);
**Notes, if you are not testing (running) both the server and the web client (browser) on the same machine, you need to change the “localhost” in the code to the actual URL or IP address of the server. Also, notes I used the port 8080 instead of the default port 80 in the sample. If you run the node.js code on your local machine, you can test it by open a browser on the same machine and browse:

Code: Select all

http://localhost:8080

Heater
Posts: 9957
Joined: Tue Jul 17, 2012 3:02 pm

Re: Server and Client side Jarvascript

Thu Sep 20, 2018 7:10 am

Do you mean Javascript?

In this post https://www.raspberrypi.org/forums/view ... 9#p1339475 I show an example of a very simple Javascript based server running under node.js interacting with button clicks on the web page.

It would not take much to add interaction with the PI GPIO pins to that client/server example. Perhaps us the rpi-pio node.js module: https://www.npmjs.com/package/rpi-gpio There are others to choose from.

There are tutorials for using node.js and Javascript in general all over the internet.
Last edited by Heater on Thu Sep 20, 2018 12:38 pm, edited 1 time in total.

DevilWAH
Posts: 14
Joined: Wed Mar 07, 2012 9:56 am
Contact: Website

Re: Server and Client side Jarvascript

Thu Sep 20, 2018 12:35 pm

Heater wrote:
Thu Sep 20, 2018 7:10 am
Do you mean Javascript?

In this post https://www.raspberrypi.org/forums/view ... 9#p1339475 I show an example of a very simple Javascript based server running under node.js interacting with button clicks on the web page.

It would not take much to add interaction with the PI GPIO pins to that client/server example. Perhaps us the rpi/gpio node.js module: https://www.npmjs.com/package/rpi-gpio There are others to choose from.

There are tutorials for using node.js and Javascript in general all over the internet.
Oh thats great and jsut what i was looking for thank you. I know there are a lot of tuturiols on the net, but many of them only cover bits and pieces. or are long complex bits of code covering many functions. This "hello world" level example is nice clear an concise to get me head around.

Return to “General programming discussion”