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 trialWilliam Ray Noble
22,190 Pointsapp.post is not a function error
Hello,
I've passed the first part of this challenge by create an app.post(), however, the second half of the challenge is telling me that app.post is not a function. I can't figure out what mistake I'm making that is generating this error. TIA
const express = require('express');
const posts = require("./mock/posts.json");
const app = express();
app.get('/', (req, res) => {
res.send("<h1>I Love Treehouse!</h1>");
});
app.post('/blog', (req, res) => {
res.send(req.body);
});
app.listen(3000, () => {
console.log("The frontend server is running on port 3000!")
});
1 Answer
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsI’m not sure why you would get an error stating that app.post is not a function. This normally happens when you try to invoke a variable (or property) with parentheses when it’s not a function.
The reason you’re not passing the challenge though is because you’re sending back the wrong info. The challenge asks you to send back the posts object, which is the variable declared on the second line. You’re sending back the body of the request. Change that and you should pass the challenge.
William Ray Noble
22,190 PointsWilliam Ray Noble
22,190 PointsThanks for response, no matter what I try I get the error app.post is not a function. I can't even make it past the first part of the challenge without receiving this error.
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsIn that case, change it to app.get. The challenge never actually asks for you to make a route for a post request, but you’ll find later in your programming that express().post is valid syntax (when you want to post to a route).
That said, if the challenge won’t allow for you to use app.post, then use app.get.