I have the following scenario:
users.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const apiRoot = '/api/v1/users';
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', false);
next();
});
app.get(apiRoot, async function(req, res) {
[... do stuff ...]
});
app.get(apiRoot + '/:id', async function(req, res){
[... do stuff ...]
});
app.listen(process.env.PORT);
bookings.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const apiRoot = '/api/v1/bookings';
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', false);
next();
});
app.get(apiRoot, async function(req, res) {
[... do stuff ...]
});
app.get(apiRoot + '/:id', async function(req, res){
[... do stuff ...]
});
app.listen(process.env.PORT);
This is working fine and without errors. Problem is that I have to maintain all my app
settings in more that one single file (I have others beyond users.js
and bookings.js
...). Is there a way I can concentrate the app
creation in one file and import it to users.js
, bookings.js
and so on?