db.js
const mongoose = require('mongoose');
const mongoURI = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB Compass&ssl=false";
const connectToMongo = ()=>{
mongoose.connect(mongoURI, ()=>{
console.log('database connected');
})
}
module.exports = connectToMongo;
index.js
const connectToMongo = require('./db');
const express = require('express');
connectToMongo();
const app = express();
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
i have checked dependencies installed nodemon using npm i -D nodemon when i typed nodemon .\index.js my live server should run but it is not[
CodePudding user response:
The reason is Powershell does not aware of nodemon. Currently nodemon is installed in your project directory.
I would suggest you to a script in package.json
"scripts": {
"start-nodemon": "nodemon ./index.js"
}
And then run in powershell
npm run start-nodemon