0

My server returns 404 when I try to submit a post request. I've stripped out nonessential logic. Nothing gets logged to the console. Somewhat at a loss here. Could the error be due to misconfiguration upstream of my server.js code? Also, a question that perhaps betrays some fundamental misunderstandings: Does it matter what url (or view) the client has accessed when pressing submit? Does the url have any bearing on the endpoint defined in the form / handled by server.js?

 const express = require('express')
 const connect = require('connect')
 const serveStatic = require('serve-static')
 const bodyParser = require('body-parser')

 const app = express()
 const port = 8080

 app.use(bodyParser.urlencoded({extended: false}))

 app.use((req, res, next) => {
   console.log('A new request received at ' + Date.now());
   next();
 });

 app.post('/', function(request, response){
    console.log(request.body);
 })

 connect()
     .use(serveStatic(__dirname))
     .listen(port, () => console.log('Server running on 8080...'))

<form action="/" id="contact-form" method="post" role="form">

  • Nothing logs at all? Don't forget to respond to the request, `res.sendStatus(200)` or something. Did you try a GET request and navigate to it with the browser before trying POST? – ggorlen Jul 07 '21 at 15:41
  • The only console entry is "POST http://x.x.x.x:8080/ 404 (Not Found)." GET works; the server is serving browsable static html – W. Bernbaum Jul 07 '21 at 15:51
  • 1
    No, I mean the logs on the server. All requests should trigger `console.log('A new request received at ' + Date.now());`. I don't see that `app.listen` was ever used, only `connect`, so you seem to be conflating two different ways of making a server. – ggorlen Jul 07 '21 at 16:07
  • 1
    Yes, it does!! Thank you for the link; was indeed conflating the two. Requests now trigger `A new request received` in the server logs, followed by empty brackets. But I believe `{ }` may be outside the scope of my question. – W. Bernbaum Jul 07 '21 at 16:45

0 Answers0