Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Timothy Williams
Timothy Williams
14,355 Points

Router posting in node with express

Ok so I have been on this one for awhile. All of my routes work in the application I am building except one.

So I'm utilizing app.use('/example') in my index file, which I have all of the appropriate imports with require("").

The file I am having issue with is the one associated with app.use('/results'). Inside this I have a route set up with router.get('/rank', (req, res) => {}). (this creates the url of website.com/results/rank which is fine, this is all working.

The problem happens when I try to post that route with router.post('/rank', (req, res) => {}). I get the error "Cannot post results/result/rank" . Also the router.post is inside app.use('/results') along with the router.get.

I have no idea where it gets the additional "result", that is the name of the js file this is saved in. I have a feeling this is an obvious fix, any insight or feedback would be greatly appreciated. Thanks.

2 Answers

Timothy Williams
Timothy Williams
14,355 Points

I am an idiot! I didn't have the action in the form pointed to the right place, the problem was in the pug file. Thank you for reminding me. The problem with working on this project at midnight! Thank you.

Can you post your result.js file that contains those routes? Can you also post the index.js file that contains the use statement? Also, are you correctly posting to /results/rank? I would need to see your code where you make the post request.

Timothy Williams
Timothy Williams
14,355 Points

I have other routes that I post to which work, so I'm thinking that I am posting correctly to results/rank, but I'm probably wrong. The file with the use statements, I was mistaken its in the app.js file and not index.js (it was late): 'use-strict';

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

const app = express();

//Set up Middleware Body-Parser app.use(bodyParser.urlencoded({ extended: false})); app.use(cookieParser());

//Set up View Engine app.set('view engine', 'pug');

//Store routes into varaibles const mainRoutes = require('./routes'); const directRoutes = require('./routes/setup'); const rankRoutes = require('./routes/calcrank'); const resultsRoutes = require('./routes/result');

//Call on the routes being used app.use(mainRoutes); app.use('/user', directRoutes); app.use('/calcrank', rankRoutes); app.use('/results', resultsRoutes);

//Create Server app.listen(process.env.PORT, () => { });

***Here is the result.js file:********** 'use-strict';

//Inport required files const express = require('express'); const router = express.Router();

router.get('/rank', (req, res) => { I removed all of the code in this block to make it easier to review, the get request is working

const templateData = {masterRank, rankHolder};
res.render('rankresults', templateData);

});

It's is here that the post request for results/rank is running into an error** router.post('/rank', (req, res) => { res.redirect('/calcmatch'); });

module.exports = router;

Hi Timothy, What are you using to do your post request with? Postman or just a simple form using a templating engine? If you are using a templating engine can you post your form html?