JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Linking Around the Application

Martin Coutts
Martin Coutts
18,154 Points

Invalid Status Code

Getting this set of data in my terminal even though from what I can see my code is correct RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports.express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10) RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports.express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10) RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports.express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10) RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports.express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10) RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports.express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10) RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:208:11) at ServerResponse._implicitHeader (_http_server.js:199:8) at write (http_outgoing.js:593:9) at ServerResponse.end (_http_outgoing.js:714:5) at ServerResponse.send (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/martincoutts5/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/martincoutts5/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports._express as engine at View.render (/Users/martincoutts5/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/martincoutts5/flashcards/node_modules/express/lib/application.js:640:10)

The code I have written for the random redirect is here

router.get('/', (req, res) => { const numberOfCards = cards.length; const flashcardId = Math.floor(Math.random() * numberofCards); res.redirect( /cards/${flashcardId}?side=question ) });

2 Answers

Adam Beer
Adam Beer
11,267 Points

You forgot to use it the backticks inside the res.redirect(``). Hope this help.

Martin Coutts
Martin Coutts
18,154 Points

Noticed that after posting also I had numberofCards instead of numberOfCards

Thanks Adam