I am using a vServer for my project, where I run my node backend with pm2. While developing I can open port 80 and 443 on localhost and everything just runs fine. When I uploaded my code to the server the pm2 service instantly crashed. Here is a picutre:
So I looked into my code
import express from 'express';
import fs from 'fs';
import http from 'http';
import https from 'http';
import * as path from 'path';
import {fileURLToPath} from 'url';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.resolve(__dirname, 'build')));
app.post('/test1', (req, res) => {
return res.send('Received a POST HTTP method');
});
app.put('/test2', (req, res) => {
return res.send('Received a PUT HTTP method');
});
app.delete('/test3', (req, res) => {
return res.send('Received a DELETE HTTP method');
});
//Start server
http.createServer(app, {
keepAlive: true
}).listen(80, () => console.info('Listening on port', 80));
//Start https server
https.createServer({
key: fs.readFileSync('./ssl/privkey.key'),
cert: fs.readFileSync('./ssl/cert.cer'),
keepAlive: true
}, app).listen(443, () => console.info('Listening on port', 443));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
});
{
"name": "dashboard-sport",
"version": "0.1.0",
"type": "module",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"http": "^0.0.1-security",
"https": "^1.0.0",
"path": "^0.12.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"url": "^0.11.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:3000"
}
My react project and index.js backend are using the same package.json. I added "type": "module" so I can use ES6 imports in both.
I'm deploying with github actions, where the projects gets buid.
Thanks in advance!