I am trying to create a Node JS app with mongoDB. from main app.js I am trying to redirect to another folder named "services". Here is my folder structure -
Here is my app.js -
const express = require('express')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const cors = require('cors')
const bodyParser = require('body-parser')
const app = express()
const users = require('./userSchema')
const services = require('./services/index')
app.use('/services', express.static('/services'))
app.use(express.static('/'));
app.use(cors())
dotenv.config()
const port = 3000
mongoose.connect(process.env.DB_CONNECT,
{
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => console.log('Connected to mongoDB'))
.catch(err => console.error('Could not connect to MongoDB..', err))
const jsonParser = bodyParser.json()
app.get('/allName', async (req, res) => {
let data = await users.find()
res.status(200).send(data)
})
app.listen(port, () => console.log(`Demo app listening on port ${port}!`))
Here is my index.js file inside services folder -
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Birds home page')
})
router.get('/about', function (req, res) {
res.send('About birds')
})
module.exports = router
While running http://localhost:3000/allName , it is working fine. But if i try to run http://localhost:3000/services, it is throwing Cannot GET /services
. I am not able to fix this.
How to redirect to index.js from app.js when users trigger http://localhost:3000/services?