2

I have created a file which creates express middle-ware app.

I have defined the app function in a separate file.

How do I run the app function.

My app.js file

const express = require('express')
const cors = require('cors')
const mysql = require('mysql')
const bodyParser = require('body-parser')
const users = require('./users')
const app = express()
app.use(cors());
app.use(bodyParser.json());
app.options('*',cors());

app.listen('3001' ,(err)=>{
  if(err)
  console.log(err);
  else{
    console.log('server is running on port 3001');
  }
})
module.exports.app = app

my other file has following contents and let say it is my-other-file.js

const app = require('./app')

//get request for all checklist details
app.get('/getallusers',async (req,res)=>{
  let sql =query('SELECT', 'users','user_deleted = 0', 'firstname,lastname,name,email,photo,admin')
  var results =await  dbQuery(res,sql)
  console.log(results);
  res.status(200).json({
    status: 200,
    message : `Found ${results.length} users`,
    users: results
  })
})
samuelj90
  • 6,484
  • 2
  • 35
  • 38

4 Answers4

1

The problem is in your app.js file you are doing a named export i.e.

module.exports.app = app;

This means when you import you need to be explicit:

const app = require('./app').app;

Instead, if you make app the default export i.e.

module.exports = app

Then you don't need to change how you require the file in my-other-file.

James
  • 75,060
  • 17
  • 154
  • 220
0

Simple answer to your question is by running following command

node my-other-file.js

if you change the

module.exports.app = app to module.exports = app in app.js file

If you are looking for a folder structure guideline then following links will help you

ExpressJS How to structure an application?

https://www.terlici.com/2014/08/25/best-practices-express-structure.html

https://medium.freecodecamp.org/how-to-write-a-production-ready-node-and-express-app-f214f0b17d8c

samuelj90
  • 6,484
  • 2
  • 35
  • 38
0

When running a nodejs file using the CLI (node myapp.js) it will only run the file code. So you have to carefully organize your files to depends on each other in the correct way.

When calling the require() statement, node will run the required js code. So you will want to have a single js file that require all the others js files dependencies.

Thus, you can run your app as the following: node my-other-file.js. It will require the app.js file, run it, then run the my-other-file code, as you expect it.

But it might not be the order you intend, so you should import the my-other-file from the app.js and run the function it defines.

app.js

const express = require('express')
const cors = require('cors')
const mysql = require('mysql')
const bodyParser = require('body-parser')
const users = require('./users')
const declareEndpoints = require('./my-other-file')
const app = express()
app.use(cors());
app.use(bodyParser.json());
app.options('*',cors());

app.listen('3001' ,(err)=>{
  if(err)
  console.log(err);
  else{
    console.log('server is running on port 3001');
  }
})
// Call the function from my-other-file
declareEndpoints(app)
module.exports.app = app

my-other-file.js

//export as function to be used from the main js file
export default function declareEndpoints(app) {
   app.get('/getallusers',async (req,res) => {
      let sql =query('SELECT', 'users','user_deleted = 'firstname,lastname,name,email,photo,admin')
      var results =await  dbQuery(res,sql)
      console.log(results);
      res.status(200).json({
         status: 200,
         message : `Found ${results.length} users`,
         users: results
      })
   })
}
Kapcash
  • 2,311
  • 1
  • 12
  • 24
0

My Answer.

App.js

const express = require('express')
const cors = require('cors')
const mysql = require('mysql')
const bodyParser = require('body-parser')
const users = require('./users')
const app = express()
app.use(cors());
app.use(bodyParser.json());
app.options('*',cors());

// Don't call listen here
// app.listen('3001' ,(err)=>{
  // if(err) console.log(err);
//   else console.log('server is running on port 3001');
// })
module.exports = app

index.js - File called by node. Eg: node index.js

const app = require("./app.js");

const port = "3001";

app.listen(port, () => console.log("Started listening"));
Naveen Vignesh
  • 1,124
  • 7
  • 19