Express - Cannot POST /signup

I have this code in my server.js file:
app.get("/signup", (request, response) => {
response.sendFile(__dirname + “/views/signup.html”);
});
app.get("/signup.html", (request, response) => {
response.sendFile(__dirname + “/views/signup.html”);
});

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});
console.log("hi");

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/views/signup.html', (req, res) => {
    console.log('Got body:', req.body);
    res.sendStatus(200);
});

But I get the error: Cannot POST /signup or Cannot POST /signup.html whenever I try and POST to it. Does anyone know how to fix this?

Eddie

Hey @EddiesTech,

what is the error that you’re getting?

Hi there,

As I said, the error I receive on the page or console (if I try to post via console) is Cannot POST /signup or Cannot POST /signup.html (depends on if I add html to the end or not (should not matter)

Eddie

Hi @EddiesTech,

I’m able to send a POST request to my glitch project using your code. Here’s what I’ve found:

  1. You’re trying to POST to /signup.html, but your path is /views/signup.html. You should try POSTing to /views/signup.html.
  2. You’re clearly defining /views/signup.html as your path, thus /views/signup.html cannot work, as it isn’t defined.

Hope this helps:)
milo

2 Likes

Thanks so much! Changed it to post to /views/signup.html and it worked! :slight_smile:

Eddie

2 Likes