Reputation: 625
I have two separate files that need to use express.js to render html
file1.js has code:
var express = require('express');
var app = express();
app.get('/foo/bar1', (req, res) => res.json(['bar1'])
Can I do the same for file2.js, with a different endpoint?
var express = require('express');
var app = express();
app.get('/foo/bar2', (req, res) => res.json(['bar2'])
Or would this cause issues with express?
Upvotes: 1
Views: 113
Reputation: 35483
You not suppose to initiate express twice, what you can do is put the endpoints handlers in a separate files, the import it them, and use the functions...
Something like that:
// file1.js
modules.exports = function handler1(req, res) {
//do stuff here
}
// file2.js
modules.exports = function handler2(req, res) {
//do stuff here
}
// app.js
const express = require('express');
const handler1 = require('./file1');
const handler2 = require('./file2');
const app = express();
app.get('/foo/bar1', handler1);
app.get('/foo/bar2', handler2);
Upvotes: 1