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 Build a REST API With Express Building API Routes in Express Building the Question Routes

I can't see the GET response in Postman

I can't seem to get the: response: "You sent me a POST request", to work in Postman. All I get is this: Cannot GET /questions

Can anybody tell me what I'm doing wrong?

Dom Farnham
Dom Farnham
19,421 Points

Can you share your code so I can take a look? :-)

7 Answers

Joel Kraft
STAFF
Joel Kraft
Treehouse Guest Teacher

Hi carina ortiz,

I'm not able to test this atm, but it looks like your connection to the routes file is incorrect, and would probably result in the message you're reporting. Seeing Cannot GET /questions means express is running, and you are connecting to it with Postman. But it can't find a route called questions. This line:

app.use("/.questions",routes);

should be

app.use("/questions",routes);

Dom Farnham Oh my F:ing god.. I found what I was doing wrong... I was writing app.use("/.questions",routes); with a dot after foward slash before questions, when I removed it, it worked. So sorry for taking up your time with my stupidity :) But thanks for your help!!!

Dom Farnham
Dom Farnham
19,421 Points

Without seeing your code, check that you are using the write method (GET/POST) in your express route. If you are sending a GET request, your express route will need to be setup to respond to GET requests instead of POST.

app.get("/questions", function (req, res) {
  res.send("You sent me a GET reeuest");
});
Dom Farnham
Dom Farnham
19,421 Points

If you are setting up a route for post requests then see below. Just make sure you switch the method from GET to POST in postman too.

app.post("/questions", function (req, res) {
  res.send("You sent me a POST reeuest");
});

yes, of course! I don't know how to paste in my screen shot, so I'll you post the code

This is my routes.js:

'use strict';

var express = require("express");
var router = express.Router();

// GET /questions
//Route frågor collection
router.get("/", function(req, res){
    res.json({response: "You sent me a GET request"});
});

// POST /questions
//Routar för att skapa frågorna
router.post("/", function(req, res){
    res.json({
            response: "You sent me a POST request",
            body: req.body
          });
});

// GET /questions/:id
//Route för specifika frågor
router.get("/:id", function(req, res){
    res.json({
            response: "You sent me a GET request for ID " + req.params.id
           });
});

module.exports = router;

And this is my app.js:

'use strict'; //För att skriva bättre kod

var express = require("express");
var app = express();
var routes = require("./routes");

var jsonParser = require("body-parser").json;

app.use(jsonParser());

app.use("/.questions",routes);


var port = process.env.PORT || 3000; //Sätter porten till 3000

app.listen(port, function() {
  console.log("Express server is listenting on port", port); //Visar medd: "Express server is listenting on port 3000" i terminalen om servern är aktiv.
});

I'm following the Build a REST API With Express by Andrew Chalkley and Joel Kraft

Dom Farnham
Dom Farnham
19,421 Points

what is the url you are using in postman?

localhost:3000/questions

Dom Farnham
Dom Farnham
19,421 Points

Try the below url as a test.

localhost:3000/questions/123

Still not working.. Thanks for trying :)

Dom Farnham
Dom Farnham
19,421 Points

Ok, so taking a closer look I can see that your route for the "You sent me a POST request" is router.post("/")

You will need to change your request to POST in postman and use localhost:3000/

Let me know how you get on. Hope this helps. :-)

Yes.. i saw that as soon as I gave up.. :) Sorry for the inconvenience guys! But thanks for your help!