BucksDad
BucksDad

Reputation: 15

I am very green when it comes to node.js integration

So I figured the best way to learn is to try and fail over and over. I am building a webapp, at least trying to. I am curious how to go about using node to query my db. I am able to make a connection to the db with my single app.js file.

var mysql = require('mysql');

    var connection = mysql.createConnection({
        host     : 'xxxxxxxx-us-east-1.rds.amazonaws.com',
        port      :  '3306',
        user     : 'user',
        password : 'password',
        database : 'app'
});
connection.connect(function(err){

    if(!err) {
        console.log("Database is connected ... ");    
    } else {
        console.log("Error connecting database ... ");    
    }
});

My problem, or lack of understand begins when I try to integrate this into my client-side js code. For instance say I wanted to trigger the db connection when a user uploads a photo.

var upload = s3.putObject({
            Bucket: albumBucketName,
            Key: photoKey,
            Body: file,
            ACL: "public-read",
        });

        var promise = upload.promise();

Can I include the app.js node file? Sorry if this is a dumb question. I feel like I am missing some fundamental understanding of how to integrate the functionality of node with my current client side JS. Any help or further reading is appreciated--I am even curious about PHP solutions. X

Upvotes: 0

Views: 75

Answers (1)

Miaplacidus
Miaplacidus

Reputation: 545

Server and client side code are separate. However you can create a Node module that harnesses the AWS and returns an appropriate response to the client after completed.

To do this, you need to create an endpoint that you post your data to from the client, then process with the same AWS modules only for Node. You also need to be able to access the connection instance from a different NodeJS module. This can be accomplished several ways. First, if the library that instantiates the connection tracks all of the connections, you should be able to require the library in a different module, then use the library's API to access one of the connections. Second, if you create only one instance of the connection and allow it to export, then you can import the module with that connection. Third, you can use something like a request/response pattern between the two modules, with the pattern instance declared globally.

Upvotes: 1

Related Questions