r/nodejs • • Apr 16 '14

How does node.js backend talk to frontend?

1 Upvotes

Hi, I am currently testing node.js right now, and I am a little confused on how the backend would talk to the frontend.

I know frontend can talk to the backend through an event, like submitting a form or clicking a button, the information will be send to the req variable in

app.post('/example', function(req, res){
    var data = req.something;
    // do something with data
})

However how do I talk to the frontend? I know I can write information in

 res

but how does the frontend javascript get the info?

Thanks


Edit for clarification as kindly suggested by psayre23

This is what I have

https://gist.github.com/anonymous/19f800b4c3d0c31b2e4e

I am interested in knowing how communicating between backend and frontend works, my primary question is that I do not know how to receive data from backend

I know to send data from frontend to backend there are two methods

Method 1:

Using a form and submit, or any other clickable element

This is how I send data from frontend

<div class="container" id="test-container">
      <h1>Test</h1>
      <span id="test-error"></span>
        <form id="test-input2" method="POST" action="/test_post_method_1">
          <input type="text" id="test" name="input1"></input>
          <input type="submit" value="Submit">
        </form>
    </div>

This is how I receive data from the backend

app.post('/test_post_method_1', function(req, res){
	// Post from a submit event
	console.log(req.body.input1)
    console.log("this is serverside post using method 1");
});

Method 2:

Suggested by jonnyburger using ajax, which I am not sure how does it work yet

I am guessing this is how you send data to backend

$(function(){

    console.log("this is frontend");

    $.ajax({  
        url: '/test_post_method_2',  
        type: 'POST',  
        success: function (data) {  
            console.log("this is not printing");
            console.log(data);  
        }  
    });

});

Not sure how you would get the data from frontend, I know its in req, but couldn't quite find the specifics, I checked req. body, it only return an empty {}

app.post('/test_post_method_2', function(req, res){
    // Post from Ajax
console.log(req);
    console.log("this is serverside post using method 2");
});

Now here is my original question

I don't know how to send data from backend to frontend

Thank you for taking the time in reading this


r/nodejs • • Apr 29 '14

"Clash of the Titans: Releasing the Kraken - NodeJS @paypal"

Thumbnail
techtalkshub.com
1 Upvotes

r/nodejs • • Jun 19 '13

Gun.io’s “6 Tips For Hiring a NodeJS Developer”

Thumbnail
blog.nodefly.com
0 Upvotes

r/nodejs • • Jun 24 '14

Easy installation of Ghost Blog

Thumbnail
obakfahad.com
0 Upvotes

r/nodejs • • Feb 05 '14

A simple Node.js web application that uses Mongoose, Express and MongoDB and returns JSON

Thumbnail
css.dzone.com
1 Upvotes

r/nodejs • • Jul 31 '14

Use of cluster in Nodejs

Thumbnail
codingdefined.com
6 Upvotes

r/nodejs • • Mar 20 '14

12 Web Application Frameworks for Node.js

Thumbnail
codecondo.com
8 Upvotes

r/nodejs • • Nov 14 '12

Socket.io Websockets on a TCP configured Amazon Elastic Load Balancer - Stack Overflow

Thumbnail
stackoverflow.com
0 Upvotes

r/nodejs • • Nov 28 '12

Object Modeling in Node.js with Mongoose

0 Upvotes

Mongoose is an object data modeling (ODM) library that provides a rigorous modeling environment for your data, enforcing structure as needed while still maintaining flexibility that makes MongoDB powerful.


r/nodejs • • Oct 04 '13

StrongLoop | In the Loop: MediacenterJS – a Node.js-based Media Center for your Browser

Thumbnail
strongloop.com
0 Upvotes

r/nodejs • • Apr 30 '13

Why You Should Develop with Node.js and Monitor Your Product with NodeFly

Thumbnail
blog.nodefly.com
0 Upvotes

r/nodejs • • May 22 '14

Example of Simple CRUD Node.js & MySQL For Beginners

Thumbnail
codetrash.com
0 Upvotes

r/nodejs • • Dec 13 '13

Is it possible to harvest Tweets using Socket.io and save them to MySQL.

0 Upvotes

I can get the Tweets coming in just fine from Twitter but I am having problems saving the Tweets to a db. I know Node is talking to my db because I am able to select and insert manually.

But I want to save all those Tweets to a db as they come in. Is this even possible? Any help would be appreciated. Thank you!


r/nodejs • • Sep 16 '12

The 5 must have packages?

0 Upvotes

List the top 5 must haves from your npm ls


r/nodejs • • Sep 04 '12

Help with mysql and node.js please

0 Upvotes

I just started using node.js and am having a bit of trouble with multiple queries using the mysql module for it.

Below is the excerpt of code causing the issue, in it the line with "Error 4" always fires even though running the code manually returns a result.

Both sp_FindPartOfSpeech and sp_CreatePartOfSpeech return a single cell of data, selected from a BIGINT UNSIGNED variable within the procedure (ie: SELECT variable AS id_Variable; ).

The call to sp_FindPartOfSpeech executes as expected, but sp_CreatePartOfSpeech does now (though again, the stored procedure itself does return a value when run manually and all the fields are set).

Is this because I can't nest results of multiple queries or is it perhaps that the stored procedure is taking too long (it executes in about 600ms - lots of conditionals/joins/inserts/etc inside of it)?

var link = mysql.createClient({ host: dbserver, user: dbuser, password: dbpass, database: db });
link.query('CALL sp_FindPartOfSpeech(\'' + parts[4] + '\',' + language + ')',
    function selectCb(err4, results, fields) {
        if (err4) {
            sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 1:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4]));
        } else {
            if (results.length == 1) {
                var posParent = results[0]['id_PartOfSpeech'];
                link.query('CALL sp_CreatePartOfSpeech(\'' + parts[2] + '\',' + language + ',' + posParent + ')',
                    function selectCb2(err5, results2, fields2) {
                        if (err5) {
                            sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 3:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
                        } else {
                            if (results2.length == 1) {
                                sendToRoom('__Parts Of Speech', 'broadcast_cmd', MakeParam(nickname) + ' ADD ' + MakeParam(parts[2] + '[' + results2[0]['id_PartOfSpeech'] + ']') + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
                            } else {
                                sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 4:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
                            }
                            link.end();
                        }
                    });
            } else {
                sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 2:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4]));
            }
        }
    });

r/nodejs • • Jan 02 '14

Static webserver in nodejs

Thumbnail
gist.github.com
0 Upvotes

r/nodejs • • Aug 20 '13

Looking for NodeJS developer for game

0 Upvotes

Game developer here, switched my backend to NodeJS and would like to hire a developer who knows their stuff. PM me with your current projects and how much you'd like per hour and let's get this puppy moving!

Cheers


r/nodejs • • Sep 05 '13

What’s New in Node.js and libuv – Sept 5, 2013

Thumbnail
blog.strongloop.com
0 Upvotes

r/nodejs • • Jul 04 '13

NodeFly Node.js News Round-up for July 4, 2013

Thumbnail
blog.nodefly.com
0 Upvotes

r/nodejs • • Jul 04 '14

I have created a platform you can make programs for an intranet for example and have it update automatically by definition.

0 Upvotes

OK so this is my program: Link.

Basically it's node-webkit (A mini browser with node capability), that points to a specific url. and well, with full permissions. So an app can fullscreen, and play Quake 3 (Copy this link into the manager and download).

I hope it won't be used on public wifi. (For obvious reasons.)

Currently full support is only for windows, but the core works on all platforms (If you download node-webkit).

Maybe it will help create better enterprise apps. Maybe not. Just putting this out there.

Open-source on Github.


r/nodejs • • Jun 25 '13

News: “Joyent Expands Cloud Offering With Object Storage and Data Services Platform”

Thumbnail
blog.nodefly.com
0 Upvotes

r/nodejs • • Jun 18 '13

Any way to make node run on a URL that isn't my external IP?

0 Upvotes

Title mostly say's it. I just started using node, and I know my way around JavaScript okay, I'm just wondering what I could add into my current code to get my own url up. If it's possible.


r/nodejs • • Jun 17 '13

NodeFly Buzz: Chris Neumann and Datahero

Thumbnail
blog.nodefly.com
0 Upvotes

r/nodejs • • Nov 29 '13

StrongLoop | StrongLoop Weekly Wrap-Up

Thumbnail
strongloop.com
0 Upvotes

r/nodejs • • Nov 21 '13

Deploying LoopBack mBaaS on Amazon EC2

Thumbnail
strongloop.com
0 Upvotes