简体   繁体   English

node.js和mongodb连接问题

[英]problem in connecting node.js and mongodb

I am building a website using node.js and mongoDB backend.我正在使用 node.js 和 mongoDB 后端构建一个网站。 Whenever I start running the server, frontend works completely fine but I am unable to connect to my database.每当我开始运行服务器时,前端都可以正常工作,但我无法连接到我的数据库。 Running mongod command always gives the message运行mongod命令总是给出消息

{"t":{"$date":"2020-11-10T10:46:27.043+5:30"},"s:"I","c":"Network","id":23016,
"ctx":"listener","msg":"Waiting for connection","attr":{"port":27017,"ssl":"off"}}

I simultaneously executed npm start from the frontend and backend folders.我同时从前端和后端文件夹执行npm start The npm start from the backend folder gives从后端文件夹npm startnpm start给出

[nodemon] starting `node ./server.js`
Listening on port 8080
MongoDB connected

The npm start from the frontend folder gives前端文件夹中的npm start给出

App running at:
- Local: http://localhost:8080/
- Network: http://192.168.56.1:8080/

But whenever the open it, it gives但是每当打开它时,它就会给出

Cannot GET /

Is it because I couldn't connect to the database?是因为我无法连接到数据库吗? I'm fairly new to this.我对此很陌生。 I can't seem to understand the reason behind this.我似乎无法理解这背后的原因。 Can anyone please help me out?任何人都可以帮我吗?

Cannot GET /

means http://localhost:8080/ has no method defined in your app.意味着http://localhost:8080/在您的应用程序中没有定义方法。 if you are using expressJs in your server.js add如果您在 server.js 中使用 expressJs 添加

var express = require('express')
var app = express()

    app.get('/', function (req, res) {
      res.send('No More error')
})

Here we are saying: send "No More error" whenever user opens "/" path.我们在这里说:每当用户打开“/”路径时发送“No More error”。

ie http://localhost:8080/ in your case.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM