I want my node js server to run when my electron app is open.
I have tried the child_process module to run a shell command like this:
const {exec} = require('child_process')
const startDBServer = exec('node ./express.js');
startDBServer.stdout.on('data', (pid)=>{
console.log(pid);
});
startDBServer.stderr.on('data', (data)=>{
console.error(data);
});
the serve is running on port 8000 but it doesn't connect to the database (when the database connect it should print "mongoose connected" on the console but it just print "App started with port 8000" with no database connection). this is my server
const express = require('express');
const app = express();
const mongoose = require('mongoose')
const cors = require('cors');
const path = require('path')
mongoose.set('strictQuery', true);
mongoose.connect('mongodb://localhost:27017/myDb')
.then(()=>{
console.log('mongoose connected')
})
.catch((err)=> console.log('error is '+ err))
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'))
app.use(cors({
origin: '*'
}));
/* All middlewares */
require('./middlewares/app.middlewares')(app, express);
/* All the routes */
require('./router/index')(app);
/* starting the app */
app.listen(8000, ()=>{
console.log('App started with port 8000')
})
//mongoose does not work with name : localhost but with the address instead
mongoose.connect('mongodb://127.0.0.1:27017/myDb')
.then(()=>{
console.log('mongoose connected')
})
.catch((err)=> console.log('error is '+ err))