When running static files through node.js, how do I call a function defined in the node.js document?










1














Lets say I have this code saved in index.js:



const serveIndex = require('serve-index');
const express = require('express');
const app = express();

app.use('/project', express.static('public'))
app.use('/project', serveIndex('public'))


app.get('/', (req, res) =>;
res.send('Server Running.');
);

function createAccount() {

// Create Account
var mysql = require("mysql");
var con = mysql.createConnection(
host: "localhost",
user: "root",
password: "",
database: "database1"
);
con.connect(function(err)
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO `users` (`username`, `password`, `email`, `confirm_code`, `confirmed`, `plugin1`, `plugin2`, `plugin3`, `plugin4`, `plugin5`, `myplugin1`, `myplugin2`, `myplugin3`, `myplugin4`, `myplugin5`) VALUES ('" + username + "', '" + password + "', '" + email + "', '" + code + "', 'no', 'none', 'none','none', 'none', 'none', 'none', 'none', 'none', 'none', 'none');";
con.query(sql, function (err, result)
if (err) throw err;
console.log("1 record inserted");
);
);

app.listen(3000, () =>; console.log('App running on port 3000.'));


And in my public file, I have a javascript file running this:



var username = "John Doe";
var password = "1234";
var email = "johndoe@example.com";
var code = "1234";

createAccount();


How do I get the createAccount() function to run properly when the js file in the public folder is run? (As in it creates the account with all the information provided in variables)










share|improve this question

















  • 1




    createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
    – Patrick Roberts
    Nov 12 '18 at 0:39















1














Lets say I have this code saved in index.js:



const serveIndex = require('serve-index');
const express = require('express');
const app = express();

app.use('/project', express.static('public'))
app.use('/project', serveIndex('public'))


app.get('/', (req, res) =>;
res.send('Server Running.');
);

function createAccount() {

// Create Account
var mysql = require("mysql");
var con = mysql.createConnection(
host: "localhost",
user: "root",
password: "",
database: "database1"
);
con.connect(function(err)
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO `users` (`username`, `password`, `email`, `confirm_code`, `confirmed`, `plugin1`, `plugin2`, `plugin3`, `plugin4`, `plugin5`, `myplugin1`, `myplugin2`, `myplugin3`, `myplugin4`, `myplugin5`) VALUES ('" + username + "', '" + password + "', '" + email + "', '" + code + "', 'no', 'none', 'none','none', 'none', 'none', 'none', 'none', 'none', 'none', 'none');";
con.query(sql, function (err, result)
if (err) throw err;
console.log("1 record inserted");
);
);

app.listen(3000, () =>; console.log('App running on port 3000.'));


And in my public file, I have a javascript file running this:



var username = "John Doe";
var password = "1234";
var email = "johndoe@example.com";
var code = "1234";

createAccount();


How do I get the createAccount() function to run properly when the js file in the public folder is run? (As in it creates the account with all the information provided in variables)










share|improve this question

















  • 1




    createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
    – Patrick Roberts
    Nov 12 '18 at 0:39













1












1








1







Lets say I have this code saved in index.js:



const serveIndex = require('serve-index');
const express = require('express');
const app = express();

app.use('/project', express.static('public'))
app.use('/project', serveIndex('public'))


app.get('/', (req, res) =>;
res.send('Server Running.');
);

function createAccount() {

// Create Account
var mysql = require("mysql");
var con = mysql.createConnection(
host: "localhost",
user: "root",
password: "",
database: "database1"
);
con.connect(function(err)
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO `users` (`username`, `password`, `email`, `confirm_code`, `confirmed`, `plugin1`, `plugin2`, `plugin3`, `plugin4`, `plugin5`, `myplugin1`, `myplugin2`, `myplugin3`, `myplugin4`, `myplugin5`) VALUES ('" + username + "', '" + password + "', '" + email + "', '" + code + "', 'no', 'none', 'none','none', 'none', 'none', 'none', 'none', 'none', 'none', 'none');";
con.query(sql, function (err, result)
if (err) throw err;
console.log("1 record inserted");
);
);

app.listen(3000, () =>; console.log('App running on port 3000.'));


And in my public file, I have a javascript file running this:



var username = "John Doe";
var password = "1234";
var email = "johndoe@example.com";
var code = "1234";

createAccount();


How do I get the createAccount() function to run properly when the js file in the public folder is run? (As in it creates the account with all the information provided in variables)










share|improve this question













Lets say I have this code saved in index.js:



const serveIndex = require('serve-index');
const express = require('express');
const app = express();

app.use('/project', express.static('public'))
app.use('/project', serveIndex('public'))


app.get('/', (req, res) =>;
res.send('Server Running.');
);

function createAccount() {

// Create Account
var mysql = require("mysql");
var con = mysql.createConnection(
host: "localhost",
user: "root",
password: "",
database: "database1"
);
con.connect(function(err)
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO `users` (`username`, `password`, `email`, `confirm_code`, `confirmed`, `plugin1`, `plugin2`, `plugin3`, `plugin4`, `plugin5`, `myplugin1`, `myplugin2`, `myplugin3`, `myplugin4`, `myplugin5`) VALUES ('" + username + "', '" + password + "', '" + email + "', '" + code + "', 'no', 'none', 'none','none', 'none', 'none', 'none', 'none', 'none', 'none', 'none');";
con.query(sql, function (err, result)
if (err) throw err;
console.log("1 record inserted");
);
);

app.listen(3000, () =>; console.log('App running on port 3000.'));


And in my public file, I have a javascript file running this:



var username = "John Doe";
var password = "1234";
var email = "johndoe@example.com";
var code = "1234";

createAccount();


How do I get the createAccount() function to run properly when the js file in the public folder is run? (As in it creates the account with all the information provided in variables)







javascript node.js server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 0:36









BGM52

328




328







  • 1




    createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
    – Patrick Roberts
    Nov 12 '18 at 0:39












  • 1




    createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
    – Patrick Roberts
    Nov 12 '18 at 0:39







1




1




createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
– Patrick Roberts
Nov 12 '18 at 0:39




createAccount() needs to accept those as parameters, and you'll need something like an HTTP POST request to send the data from the client to the server.
– Patrick Roberts
Nov 12 '18 at 0:39












1 Answer
1






active

oldest

votes


















0














There are a few ways to do it. The simplest way is to just export that information to your main index.js file and then pass it into your createAccount() function. With the way you currently have that index.js file, all of the username, password etc are undefined. So you'd have to import them from your public file at the very least.



The proper way to do this, though, would be to create a function that sends an HTTP(S) POST request to wherever your web server is located along with the relevant information. Then, you just make a callback function that will accept the option params in the request and pass them along into your createAccount() function, which will in turn execute the SQL command as you wrote it already in your OP.






share|improve this answer




















  • I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
    – BGM52
    Nov 12 '18 at 0:52










  • This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
    – M. L.
    Nov 12 '18 at 1:01










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254660%2fwhen-running-static-files-through-node-js-how-do-i-call-a-function-defined-in-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














There are a few ways to do it. The simplest way is to just export that information to your main index.js file and then pass it into your createAccount() function. With the way you currently have that index.js file, all of the username, password etc are undefined. So you'd have to import them from your public file at the very least.



The proper way to do this, though, would be to create a function that sends an HTTP(S) POST request to wherever your web server is located along with the relevant information. Then, you just make a callback function that will accept the option params in the request and pass them along into your createAccount() function, which will in turn execute the SQL command as you wrote it already in your OP.






share|improve this answer




















  • I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
    – BGM52
    Nov 12 '18 at 0:52










  • This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
    – M. L.
    Nov 12 '18 at 1:01















0














There are a few ways to do it. The simplest way is to just export that information to your main index.js file and then pass it into your createAccount() function. With the way you currently have that index.js file, all of the username, password etc are undefined. So you'd have to import them from your public file at the very least.



The proper way to do this, though, would be to create a function that sends an HTTP(S) POST request to wherever your web server is located along with the relevant information. Then, you just make a callback function that will accept the option params in the request and pass them along into your createAccount() function, which will in turn execute the SQL command as you wrote it already in your OP.






share|improve this answer




















  • I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
    – BGM52
    Nov 12 '18 at 0:52










  • This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
    – M. L.
    Nov 12 '18 at 1:01













0












0








0






There are a few ways to do it. The simplest way is to just export that information to your main index.js file and then pass it into your createAccount() function. With the way you currently have that index.js file, all of the username, password etc are undefined. So you'd have to import them from your public file at the very least.



The proper way to do this, though, would be to create a function that sends an HTTP(S) POST request to wherever your web server is located along with the relevant information. Then, you just make a callback function that will accept the option params in the request and pass them along into your createAccount() function, which will in turn execute the SQL command as you wrote it already in your OP.






share|improve this answer












There are a few ways to do it. The simplest way is to just export that information to your main index.js file and then pass it into your createAccount() function. With the way you currently have that index.js file, all of the username, password etc are undefined. So you'd have to import them from your public file at the very least.



The proper way to do this, though, would be to create a function that sends an HTTP(S) POST request to wherever your web server is located along with the relevant information. Then, you just make a callback function that will accept the option params in the request and pass them along into your createAccount() function, which will in turn execute the SQL command as you wrote it already in your OP.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 0:47









M. L.

261




261











  • I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
    – BGM52
    Nov 12 '18 at 0:52










  • This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
    – M. L.
    Nov 12 '18 at 1:01
















  • I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
    – BGM52
    Nov 12 '18 at 0:52










  • This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
    – M. L.
    Nov 12 '18 at 1:01















I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
– BGM52
Nov 12 '18 at 0:52




I'm pretty new to node.js, so is there anywhere you can link me where there is documentation for this?
– BGM52
Nov 12 '18 at 0:52












This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
– M. L.
Nov 12 '18 at 1:01




This is what you would use for the POST request: nodejs.org/docs/latest-v9.x/api/… (though alternatively you could just use Express)
– M. L.
Nov 12 '18 at 1:01

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254660%2fwhen-running-static-files-through-node-js-how-do-i-call-a-function-defined-in-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Peter Parker: The Spectacular Spider-Man #308