0

I have an app.js main file:

const express = require('express')
const app = express()
app.use(express.json())

var bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({
    extended: true
}))

var multer = require('multer')
var path = require('path')  

app.get('/', function (req, res) {
    // Do something 
})

app.get('/api/get_entities', function (req, res) {
    // Do something else
})  

How can I break this down into different files. Maybe something like:

main_imports.js

const express = require('express')
const app = express()
app.use(express.json())

var bodyParser = require('body-parser')  

do_this.js

app.use(bodyParser.urlencoded({
    extended: true
}))  

this_too.js

app.get('/api/get_entities', function (req, res) {
    // Do something else
})  

all_app.js

main_imports.js
do_this.js  
this_too.js

Thank you all in advance.

UPDATE

I am looking for something that can work something alike PHP include (or require) so that I don't have to use namespaces.

I would like the final file to be a dump of all functions, and not have to do:

var do_this = require('./do_this');
console.log(typeof do_this.foo);  

and instead just have:

all_app.js finally having:

const express = require('express')
const app = express()
app.use(express.json())

var bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({
    extended: true
}))

var multer = require('multer')
var path = require('path')  

app.get('/', function (req, res) {
    // Do something 
})

app.get('/api/get_entities', function (req, res) {
    // Do something else
})  
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • umm, have you checked [export and include](https://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files)? – Bagus Tesa Dec 27 '18 at 06:32
  • you can check [multiple files](https://stackoverflow.com/questions/8752627/how-can-i-split-a-javascript-application-into-multiple-files) – Mohammad Malek Dec 27 '18 at 06:35
  • I am looking for something that can work something alike **PHP** `include` (or `require`) so that I don't have to use namespaces. – Program-Me-Rev Dec 27 '18 at 06:38
  • you are currently using `require` or I get something wrong? – apple apple Dec 27 '18 at 06:39
  • 1
    are you using an automation tool like grunt or gulp? you can concatenate these files into one with uglifyjs. – Taha Paksu Dec 27 '18 at 06:42

1 Answers1

0
fs.readFile(__dirname+'/app.js','utf8',(err,data)=>{
        cuts=data.split('\n'); //split the data w.r.t line breaks and store in an array


        })

Now the contents of the js file are stored in an array. Get the index of the points which you want to include in your file. Then-

 fs.writeFileSync(__dirname+'/main_imports.js',cuts.splice(0,x));

Where x is the index of the last line or your file. Repeat this process for all the files you want to create. This can be done in a loop also, but you want all file names to be different so manually.

ellipsis
  • 12,049
  • 2
  • 17
  • 33