如何只在启动桌面电子应用程序时运行本地Node js服务器?



我希望我的node js服务器在我的电子应用程序打开时运行。

我已经尝试了child_process模块运行shell命令,像这样:

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);
});

服务器在端口8000上运行,但它没有连接到数据库(当数据库连接时,它应该打印"mongoose connected"但它只会打印"App started with port 8000"没有数据库连接)。这是我的服务器

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))

相关内容

  • 没有找到相关文章

最新更新