1

I have two separate files that need to use express.js to render html

  • file1.js
  • file1.js

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.