I assume you have more on your file so you want to separate it, as it is the code you want to move needs all the other requires as dependencies in order to work.
If you only want the certificate parts in other file you could try this:
// certificate.js
const fs = require('fs');
const certificates = {
cert: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/cert.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/privkey.pem'),
};
module.exports = certificates;
And in your app.js file:
// app.js
const https = require('https');
const express = require('express');
const certificates = require('./certificates.js');
const app = express();
const server = https.createServer(certificates, app);
server.listen(443);
other option
To separate everything you asked in the OP, you would need to move almost everything, which I don't see the reason, but if you want it anyway you could try this:
// certificate.js
const fs = require('fs');
const https = require('https');
const express = require('express');
const app = express();
const server = https.createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/cert.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/privkey.pem'),
}, app);
module.exports = server;
See that as the code has almost all the dependencies we moved most of it to the new file.
// app.js
const server = require('./certificates.js');
server.listen(443);