-2

Getting:

ReferenceError: app is not defined.

Here is the error details:

\[nodemon\] restarting due to changes...
\[nodemon\] starting `node server.js`

D:\\ebl\\server\\app\\routes.js:4
app.get('/health', (\_req, res) =\> {
^

ReferenceError: app is not defined
at Object.\<anonymous\> (D:\\ebl\\server\\app\\routes.js:4:1)
at Module.\_compile (node:internal/modules/cjs/loader:1241:14)
at Module.\_extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module.\_load (node:internal/modules/cjs/loader:938:12)
at Module.require (node:internal/modules/cjs/loader:1115:19)
at require (node:internal/modules/helpers:130:18)
at Object.\<anonymous\> (D:\\ebl\\server\\app\\app.js:8:9)
at Module.\_compile (node:internal/modules/cjs/loader:1241:14)
at Module.\_extensions..js (node:internal/modules/cjs/loader:1295:10)

Node.js v20.9.0
\[nodemon\] app crashed - waiting for file changes before starting...

Source code:
app.js

require('dotenv').config('../.env')
const express = require('express')
const app = express();

app.use(require('./middleware'));
app.use(require('./routes'));
app.use(require('./error'));

module.exports = app;

routes.js

 const router = require('express').Router();

 app.get('/health', (_req, res) => {
 res.status(200).json({ message: 'success' })
 });

 module.exports = router

package.json

{
  "name": "ebl",
  "version": "1.0.0",
  "description": "ebl",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon server.js"
  },
  "keywords": [],
  "author": "ebl",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "mongoose": "^8.3.5",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.0"
  }
}

I chanage the path route '/health' to another name '/home', but error is the same

2
  • 1
    router.get('/health', ...) in routes.js Commented May 17, 2024 at 19:48
  • 1
    I am voting to close this question as Not Reproducible or Caused by a Typo as noted in the comment by @moonwave99 as well as the answer by Subha - "Since the route.js exports router it should be router.get('.. (instead of app.get('..)" Commented Jul 30 at 3:28

1 Answer 1

1

Since the route.js exports router it should be router.get('.. (instead of app.get('..):

router.get('/health', (_req, res) => {...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.