Is it possible to run 2 applications at the same time in the same main file? #4796
Answered
by
micalevisk
scriptyyy13
asked this question in
Q&A
-
Is it possible to run 2 applications at the same time in the same main file? If so, how? |
Beta Was this translation helpful? Give feedback.
Answered by
micalevisk
Jan 26, 2022
Replies: 2 comments 2 replies
-
You can just call const express = require('express')
const app1 = express()
app1.get('/', function (req, res) {
res.send('foo')
})
app1.listen(3000)
const app2 = express()
app2.get('/', function (req, res) {
res.send('bar')
})
app2.listen(3001) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
scriptyyy13
-
You can have multiple const express = require("express");
const https = require("https");
const http = require("http");
const serverOptions = {
cert: fs.readFileSync("/path/to/cert"),
key: fs.readFileSync("/path/to/privkey")
};
const app = express();
// add your routes to app
// Main HTTPS server
const server = https.createServer(serverOptions, app).listen(443, () => {
console.log("HTTPS server running");
});
// Start server on port 80, which will redirect users to HTTPS
http.createServer((req, res) => {
res.writeHead(301, { "Location": config.url + (req.url || "/") }).end();
}).listen(80, () => {
console.log("Redirect to HTTPS running on port 80");
});
// with another app
const app2 = express();
// add routes to app2
const server = app.listen(8081, () => {
console.log("app2 HTTP server running");
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can just call
express
twice