I'm trying to build a demo application and having "Cannot GET /" error when I try to reach out http://localhost:8800/
On the other hand, the output as expected:
Connected to backend.
mongoDB connected!
Connected to mongoDB.
So I couldn't understand where exactly I have the problem, do you have any suggestions?
import mongoose from "mongoose";
import express from "express";
import authRoute from "./routes/auth.js";
import usersRoute from "./routes/users.js";
import hotelsRoute from "./routes/hotels.js";
import roomsRoute from "./routes/rooms.js";
const app = express();
// dotenv.config()
const connect = async ()=>{
try {
await mongoose.connect('mongodb srv://********@******.mongodb.net/?retryWrites=true&w=majority');
// Check how we can use .env file
console.log("Connected to mongoDB.")
} catch (error) {
throw error
}
};
mongoose.connection.on("disconnected", ()=>{
console.log("mongoDB disconnected!")
})
mongoose.connection.on("connected", ()=>{
console.log("mongoDB connected!")
})
app.use(express.json())
app.use("api/auth", authRoute);
app.use("api/users", usersRoute);
app.use("api/hotels", hotelsRoute);
app.use("api/rooms", roomsRoute);
app.listen(8800, ()=>{
connect()
console.log("Connected to backend.")
});
CodePudding user response:
You don't need a root route. However, if you still want something to display, you can just add in your code :
app.get('/', (_, res) => res.send({message: 'Hello from Express'}))
CodePudding user response:
Your code is running, it just doesn't have a route at '/', which is your path when you go to any website. if you want to render something on the page, just add
app.get('/', (req, res)=>{
res.send('this text will now appear at localhost:8800');
/*
you could also import path and do
path.join(__dirname 'yourfilename.html') to render an html file
*/
})
CodePudding user response:
you can make '/' root
app.get('/', (req, res) => {
res.send('Hello World!')
})