0

I'm using Node.js and Express and I'm getting an error that says this:

Failed to load resource: the server responded with a status of 404 (Not Found) [http://localhost:3000/recentbroadcasts/12-10-2018]

It was working, and then I tried to add a date parameter to the url and I started getting a 404. What am I missing?

Below is the code from the webpage, the app.js and the router file.

I changed the route from /recentBroadcasts to /recentbroadcasts/:date to try and get some info to make a call to my database. Then I started getting the error.

Do I have to do something special to use a route parameter? Thanks

Route called from webpage Javascript

let getRecentBroadcasts = function (lengthOfBroadcastHistoryNeeded) {

lengthOfBroadcastHistoryNeeded+=7;
let dateOfLastBroadcastNeeded = moment().subtract(lengthOfBroadcastHistoryNeeded, 'days').calendar();
dateOfLastBroadcastNeeded = moment(dateOfLastBroadcastNeeded).format('MM-DD-YYYY');

  fetch('/recentbroadcasts/'+ dateOfLastBroadcastNeeded)
    .then(response => response.json())
    .then(recentBroadcastsJson => {
      broadcastListData = recentBroadcastsJson;
      populateRecents();
    })
    .catch(error => {
      console.log(error)
    });
};

app.js File

let createError = require('http-errors'); 
let express = require('express'); 
let path = require('path'); 
let cookieParser = require('cookie-parser'); 
let logger = require('morgan'); 
let bodyParser = require('body-parser');


let indexRouter = require('./routes/index'); let usersRouter = require('./routes/users');

let app = express();

app.locals.moment = require('moment');

// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'pug');

app.use(logger('dev')); 
app.use(express.json()); 
app.use(express.urlencoded({extended: false})); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public')));

// Make our db accessible to our router 
app.use(function(req, res, next) {   req.db = db;   next(); });


app.use('/', indexRouter); 
app.use('/users', usersRouter);

// catch 404 and forward to error handler 
app.use(function(req, res, next) {   next(createError(404)); });


module.exports = app;

index.js File

let express = require('express');

let router = express.Router();

    router.get('/recentbroadcasts/:date', function (req, res) {  
    let db = req.db;   let collection = db.get('broadcastCollection');   let historyNeeded = req.params.date;   
    let a = moment(historyNeeded);  
    let b = moment().date();      
    let daysOfHistory = a.diff(b, 'days');   
    let dbQuery = { "broadcastAirDate": { $gte: new Date((new Date().getTime() - (daysOfHistory * 24 * 60 * 60 * 1000))), $lte: new Date((new Date().getTime())) } };   

collection.find(dbQuery, {}, function (e, docs) {
        let sortedBroadcastData = docs.sort(sortDesc("broadcastAirDate"));
        res.json(sortedBroadcastData);   }); });

    module.exports = router;
Matt Pell
  • 31
  • 2

1 Answers1

0

So after some research, I think I figured out what's happening. From searching stack overflow, using the tags I tagged my question with, I came up with this: https://expressjs.com/en/guide/routing.html#route-parameters The documentation for the router. Which if I'm reading correctly, basically says that I need a new route parameter after each hyphen.

So the fetch call

dateOfLastBroadcastNeeded = moment(dateOfLastBroadcastNeeded).format('MM-DD-YYYY');
fetch('/recentbroadcasts/'+ dateOfLastBroadcastNeeded)

needs a router that looks for 3 parameters, like so:

router.get('/recentbroadcasts/:month-:date-:year', function (req, res) {

I'll try it out, and if it doesn't work, I'll come back here and delete this answer. Otherwise I'll leave this answer for anyone else who comes across this same problem.

Matt Pell
  • 31
  • 2