Cannot GET /.netlify/functions/index error from deployed express backend

Below is my code inside functions folder >

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const serverless = require('serverless-http')
const { MongoClient, ObjectId } = require('mongodb')
const router = express.Router()


const app = express()



app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('./netlify/functions/index',router)
// app.use(init())

app.use(cors());
const corsOptions = {
  origin: "https://zippy-biscuit-910a4c.netlify.app"
};


const connectionUrl = `xxxx`
const dbName = 'juhosi-db'

let db


const init = () =>
    MongoClient.connect(connectionUrl, { useNewUrlParser: true }).then((client) => {
        db = client.db(dbName)
    })


const getItemById = (id) => {
    const collection = db.collection('customers')
    return collection.find({ customerId: id }).toArray()
}

const insertItem = (item) => {
    // console.log("item ",item)
    const collection = db.collection('orders')
    return collection.insertOne(item)
}

const getItemByIdAdmin = (id) => {
    const collection = db.collection('orders')
    return collection.find().toArray()
}



router.get('/', cors(corsOptions), (req, res) => {
  res.json({
      hello: "hi!"
    });
})

router.get('/login/:admin', cors(corsOptions), (req, res) => {
  const id = req.params.admin;
  getItemById(id)
      .then((items) => {
          res.json(items)
      })
      .catch((err) => {
          console.log(err)
          res.status(500).end()
      })
})

router.post('/storeData', cors(corsOptions), (req, res) => {
  const item = req.body
  // console.log("Hello")
  // console.log(req.body)
  if (item.error) {
      console.log(item.error)
      res.status(400).end()
      return
  }
  insertItem(item)
      .then(() => {
          res.send(item._id)
          res.status(200).end()
      })
      .catch((err) => {
          console.log(err)
          res.status(500).end()
      })
})
var quantity1 = 0;
var weight1 = 0;
var boxC1 = 0;

var quantity2 = 0;
var weight2 = 0;
var boxC2 = 0;

router.get('/admin', cors(corsOptions), (req, res) => {
  const id = req.params.admin;
  getItemByIdAdmin(id)
      .then((items) => {

          items.map((item) =>
              item.owner === "customer1" ? (quantity1 += parseInt(item.quantity),
                  weight1 += parseInt(item.weight1),
                  boxC1 += parseInt(item.boxCount)) : 0

          )

          items.map((item) =>
              item.owner === "customer2" ? (quantity2 += parseInt(item.quantity),
                  weight2 += parseInt(item.weight1),
                  boxC2 += parseInt(item.boxCount)) : 0

          )
          // console.log({quantity1,weight1,boxC1,quantity2,weight2,boxC2})
          res.json({ quantity1, weight1, boxC1, quantity2, weight2, boxC2 })
      })
      .catch((err) => {
          console.log(err)
          res.status(500).end()
      })
})




init().then(() => {
  
  // console.log('Server running')
})

module.exports = app;
module.exports = router;
module.exports.handler = serverless(app)

My package.json is as follows >

{
  "name": "juhosi-app-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
  "start": "cross-env NODE_ENV=dev npm-run-all --parallel start:app start:server",
    "start:app": "react-scripts start",
    "start:server": "netlify-lambda serve functions",
    "build": "npm-run-all --parallel build:**",
    "build:app": "react-scripts build",
    "build:functions": "netlify-lambda build functions",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Geethika S",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "mongodb": "^5.6.0",
    "mysql": "^2.18.1",
    "netlify-lambda": "^2.0.16",
    "serverless-http": "^3.2.0"
  }
}

The project is getting deployed but when I go to the deployed url, it gives

Cannot GET /.netlify/functions/index
bbb

b

My folder structure

@Geethu-95 Do you have the functions directory being set in the netlify.toml?

According to the documentation the default is YOUR_BASE_DIRECTORY/netlify/functions

See:

@nathanmartin Yes , the code is as below in the netlify.toml file >

[build]
functions = “functions”

Got it ! The line was wrong,corrected it to >

app.use(‘/.netlify/functions/index’,router)

Excellent, it’s hard to guess at peoples issues with limited visibility.

Great work self-solving.

2 Likes

Thanks for coming back and sharing your solution with the community.

1 Like