how to include multiple js files in app.js in node?










0














Here I have two js files first is app.js and second is secondfile.js.



app.js file is my main file in app.js file I wrote database connectivity code and other basic code. Now I include secondfile.js in app.js file and in secondfile.js I wrote one API but it doesn't work (it gives this type of errors ReferenceError: app is not defined) how to include secondfile.js in app.js?



app.js



var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();
const http = require('http');
const server = require('http').createServer(app);
var secondfile = require('./secondfile');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded( extended: false ));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: false ));
app.use(cors(Origin: 'http://127.0.0.1:3000'));

var connection = mysql.createConnection(
host : 'localhost',
user : 'root',
password : '',
database : 'rapnets',
);

connection.connect();

server.listen(3000,function(req,res)
console.log('Connect on 3000');
);

module.exports = app;


secondfile.js



app.post('/getAllUsersDetails',function(req,res)
connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
if (error) throw error;
res.send(
'status':'1',
'success': 'true',
'payload': results,
'message':'user details is selected'
);
);
);









share|improve this question




























    0














    Here I have two js files first is app.js and second is secondfile.js.



    app.js file is my main file in app.js file I wrote database connectivity code and other basic code. Now I include secondfile.js in app.js file and in secondfile.js I wrote one API but it doesn't work (it gives this type of errors ReferenceError: app is not defined) how to include secondfile.js in app.js?



    app.js



    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');
    var indexRouter = require('./routes/index');
    var usersRouter = require('./routes/users');
    var mysql = require('mysql');
    var bodyParser = require('body-parser');
    var cors = require('cors');
    var app = express();
    const http = require('http');
    const server = require('http').createServer(app);
    var secondfile = require('./secondfile');

    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'pug');
    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded( extended: false ));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use('/', indexRouter);
    app.use('/users', usersRouter);
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded( extended: false ));
    app.use(cors(Origin: 'http://127.0.0.1:3000'));

    var connection = mysql.createConnection(
    host : 'localhost',
    user : 'root',
    password : '',
    database : 'rapnets',
    );

    connection.connect();

    server.listen(3000,function(req,res)
    console.log('Connect on 3000');
    );

    module.exports = app;


    secondfile.js



    app.post('/getAllUsersDetails',function(req,res)
    connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
    if (error) throw error;
    res.send(
    'status':'1',
    'success': 'true',
    'payload': results,
    'message':'user details is selected'
    );
    );
    );









    share|improve this question


























      0












      0








      0







      Here I have two js files first is app.js and second is secondfile.js.



      app.js file is my main file in app.js file I wrote database connectivity code and other basic code. Now I include secondfile.js in app.js file and in secondfile.js I wrote one API but it doesn't work (it gives this type of errors ReferenceError: app is not defined) how to include secondfile.js in app.js?



      app.js



      var createError = require('http-errors');
      var express = require('express');
      var path = require('path');
      var cookieParser = require('cookie-parser');
      var logger = require('morgan');
      var indexRouter = require('./routes/index');
      var usersRouter = require('./routes/users');
      var mysql = require('mysql');
      var bodyParser = require('body-parser');
      var cors = require('cors');
      var app = express();
      const http = require('http');
      const server = require('http').createServer(app);
      var secondfile = require('./secondfile');

      // view engine setup
      app.set('views', path.join(__dirname, 'views'));
      app.set('view engine', 'pug');
      app.use(logger('dev'));
      app.use(express.json());
      app.use(express.urlencoded( extended: false ));
      app.use(cookieParser());
      app.use(express.static(path.join(__dirname, 'public')));
      app.use('/', indexRouter);
      app.use('/users', usersRouter);
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded( extended: false ));
      app.use(cors(Origin: 'http://127.0.0.1:3000'));

      var connection = mysql.createConnection(
      host : 'localhost',
      user : 'root',
      password : '',
      database : 'rapnets',
      );

      connection.connect();

      server.listen(3000,function(req,res)
      console.log('Connect on 3000');
      );

      module.exports = app;


      secondfile.js



      app.post('/getAllUsersDetails',function(req,res)
      connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
      if (error) throw error;
      res.send(
      'status':'1',
      'success': 'true',
      'payload': results,
      'message':'user details is selected'
      );
      );
      );









      share|improve this question















      Here I have two js files first is app.js and second is secondfile.js.



      app.js file is my main file in app.js file I wrote database connectivity code and other basic code. Now I include secondfile.js in app.js file and in secondfile.js I wrote one API but it doesn't work (it gives this type of errors ReferenceError: app is not defined) how to include secondfile.js in app.js?



      app.js



      var createError = require('http-errors');
      var express = require('express');
      var path = require('path');
      var cookieParser = require('cookie-parser');
      var logger = require('morgan');
      var indexRouter = require('./routes/index');
      var usersRouter = require('./routes/users');
      var mysql = require('mysql');
      var bodyParser = require('body-parser');
      var cors = require('cors');
      var app = express();
      const http = require('http');
      const server = require('http').createServer(app);
      var secondfile = require('./secondfile');

      // view engine setup
      app.set('views', path.join(__dirname, 'views'));
      app.set('view engine', 'pug');
      app.use(logger('dev'));
      app.use(express.json());
      app.use(express.urlencoded( extended: false ));
      app.use(cookieParser());
      app.use(express.static(path.join(__dirname, 'public')));
      app.use('/', indexRouter);
      app.use('/users', usersRouter);
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded( extended: false ));
      app.use(cors(Origin: 'http://127.0.0.1:3000'));

      var connection = mysql.createConnection(
      host : 'localhost',
      user : 'root',
      password : '',
      database : 'rapnets',
      );

      connection.connect();

      server.listen(3000,function(req,res)
      console.log('Connect on 3000');
      );

      module.exports = app;


      secondfile.js



      app.post('/getAllUsersDetails',function(req,res)
      connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
      if (error) throw error;
      res.send(
      'status':'1',
      'success': 'true',
      'payload': results,
      'message':'user details is selected'
      );
      );
      );






      node.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:16









      Paul T. Rawkeen

      3,20522439




      3,20522439










      asked Nov 12 at 14:11









      Amisha Rana

      15011




      15011






















          1 Answer
          1






          active

          oldest

          votes


















          1














          In the secondFile export a function that adds the post API:



          module.exports.setupApi = function(app) 
          app.post('/getAllUsersDetails',function(req,res)
          connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
          if (error) throw error;
          res.send(
          'status':'1',
          'success': 'true',
          'payload': results,
          'message':'user details is selected'
          );
          );
          );



          In the app.js use second file as:



          var secondfile = require('./secondfile');
          ........
          secondfile.setupApi(app);





          share|improve this answer




















          • not works now i not getting error but not works
            – Amisha Rana
            Nov 12 at 14:37










          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%2f53263950%2fhow-to-include-multiple-js-files-in-app-js-in-node%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









          1














          In the secondFile export a function that adds the post API:



          module.exports.setupApi = function(app) 
          app.post('/getAllUsersDetails',function(req,res)
          connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
          if (error) throw error;
          res.send(
          'status':'1',
          'success': 'true',
          'payload': results,
          'message':'user details is selected'
          );
          );
          );



          In the app.js use second file as:



          var secondfile = require('./secondfile');
          ........
          secondfile.setupApi(app);





          share|improve this answer




















          • not works now i not getting error but not works
            – Amisha Rana
            Nov 12 at 14:37















          1














          In the secondFile export a function that adds the post API:



          module.exports.setupApi = function(app) 
          app.post('/getAllUsersDetails',function(req,res)
          connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
          if (error) throw error;
          res.send(
          'status':'1',
          'success': 'true',
          'payload': results,
          'message':'user details is selected'
          );
          );
          );



          In the app.js use second file as:



          var secondfile = require('./secondfile');
          ........
          secondfile.setupApi(app);





          share|improve this answer




















          • not works now i not getting error but not works
            – Amisha Rana
            Nov 12 at 14:37













          1












          1








          1






          In the secondFile export a function that adds the post API:



          module.exports.setupApi = function(app) 
          app.post('/getAllUsersDetails',function(req,res)
          connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
          if (error) throw error;
          res.send(
          'status':'1',
          'success': 'true',
          'payload': results,
          'message':'user details is selected'
          );
          );
          );



          In the app.js use second file as:



          var secondfile = require('./secondfile');
          ........
          secondfile.setupApi(app);





          share|improve this answer












          In the secondFile export a function that adds the post API:



          module.exports.setupApi = function(app) 
          app.post('/getAllUsersDetails',function(req,res)
          connection.query('SELECT userid, username,inserteddate from user', function (error, results, fields)
          if (error) throw error;
          res.send(
          'status':'1',
          'success': 'true',
          'payload': results,
          'message':'user details is selected'
          );
          );
          );



          In the app.js use second file as:



          var secondfile = require('./secondfile');
          ........
          secondfile.setupApi(app);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 14:23









          Ankur

          30k23258




          30k23258











          • not works now i not getting error but not works
            – Amisha Rana
            Nov 12 at 14:37
















          • not works now i not getting error but not works
            – Amisha Rana
            Nov 12 at 14:37















          not works now i not getting error but not works
          – Amisha Rana
          Nov 12 at 14:37




          not works now i not getting error but not works
          – Amisha Rana
          Nov 12 at 14:37

















          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%2f53263950%2fhow-to-include-multiple-js-files-in-app-js-in-node%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線