0

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 -

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?

2 Answers2

2

change

app.use('/services', express.static('/services'))

into

app.use('/services', services);

express.static is used to serve static files, looks like you wish to use a router and not return static files. This is why the server does not respond as you like

R. Gulbrandsen
  • 3,648
  • 1
  • 22
  • 35
1

Yes, because you haven't properly added the reference of the service routes.
Remove express.static from the reference because you already have imported the service routes in a variable then just use it and it will work as expected. Just a note. Express.static is used to load/use the static files like css or images or something like that. Check the code below.

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', services)** // change this into your code.
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}!`))
Juhil Somaiya
  • 873
  • 7
  • 20