16

I am getting the above error when running the following code in my express app. When I go to localhost:3000/cards.

This is in my cards.js file.

const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
const { cards } = data;

router.get('/', (req, res) => {
res.render('card', {
prompt: cards[0].question,
hint: cards[0].hint
 });
});

module.exports = router;

This is the code in my app.js file.

const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

 app.set('view engine', 'pug');

 const mainRoutes = require('./routes');
 const cardRoutes = require('./routes/cards');

 app.use(mainRoutes);
 app.use('/cards', cardRoutes);

 app.use((req, res, next) => {
 const err = new Error('Not Found');
 err.status = 404;
 next(err);
 });

app.use((err, req, res, next) => {
res.locals.error = err;
res.status(err.status);
res.render('error');
});

 app.listen(3000, () => {
 console.log('The application is running on localhost:3000!');
 });

Any help would be appreciated. Basically, I am trying to incorporate a json file into the express app and it is coming up with this error.

AltBrian
  • 2,392
  • 9
  • 29
  • 58

3 Answers3

20

Set a default status in your error handler middleware if error does not contain the property status.

const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.set('view engine', 'pug');

const mainRoutes = require('./routes');
const cardRoutes = require('./routes/cards');

app.use(mainRoutes);
app.use('/cards', cardRoutes);

app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use((err, req, res, next) => {
  res.locals.error = err;
  const status = err.status || 500;
  res.status(status);
  res.render('error');
});

app.listen(3000, () => {
  console.log('The application is running on localhost:3000!');
});
Korbinian Kuhn
  • 726
  • 4
  • 9
  • this const status = err.status || 500; helped me :) thanks a lot – Mohammed Ramadan Jul 12 '20 at 22:55
  • what if err.status is something invalid (bcoz of typo) like err.status=1 or 24 – Nik Nov 22 '21 at 04:38
  • Express will throw an error and send status code 500 + message + stack. If you want to send a custom response, you have 3 options: 1. wrap res.status() in try/catch, 2. use a second error middleware , 3. validate status against the builtin http.STATUS_CODES list. – Korbinian Kuhn Nov 22 '21 at 10:11
0

In My Case this Happened because I haven't provided Authorization in My Request :)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '23 at 10:29
0

instead use res.render() if your are using ejs to create the template;

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 18 '23 at 08:10