Solution 1 :

You can do it but will have to be done programmatically and not implicitly through the action attribute. For that, you can use then/catch from Promises or try/catch with async/await like the example below:

      <input type="text" name="fname" />
      <br />

      <input type="text" name="lname" />
      <br />

      <button type="submit">Send</button>

      const form = document.querySelector('form');

      form.addEventListener('submit', async (event) => {

        const elements =;
        const firstName = elements['fname'].value;
        const lastName = elements['lname'].value;
        const dataToSend = { firstName, lastName };

        try {
          const response = await fetch('/example', {
            method: 'POST',
            body: JSON.stringify(dataToSend),
            headers: { 'Content-Type': 'application/json' },
          const data = await response.json();

          console.log('Data returned from the server', data);
        } catch (error) {
          console.log('Uups! Something went wrong', error);

Problem :

Assume that I have a form as such that is submitted to a POST route /example:

<form action="/example" method="POST">
  <input type="text" name="fname"><br>
  <input type="text" name="lname"><br>
  <button type="submit">Send</button>

app.js:'/example', (req, res) => {


const app = require('./app');

app.listen(3000, () => {
    console.log('server is running on port 3000');

Does Javascript provide a way to prevent the form submission if the server disconnects in order to prevent the website from crashing?

I have searched online but couldn’t find any such solutions to this problem.


Comment posted by David

You could submit the form via AJAX and catch/handle various errors/failures in the client-side code.

Comment posted by bgmn

Would server disconnection pass as an error if I try to

Comment posted by David

If by “server disconnection” you mean that the server doesn’t respond then I imagine that would result in some kind of identifiable error. It depends on how you’re using AJAX, such as what client-side library you may be using or just manually using it. However you’re using AJAX, you can find examples of how to handle errors and then start testing that specific error.

Comment posted by bgmn

Am I right in thinking that

Comment posted by bgmn

I seem to be getting

Comment posted by lndgalante

Yes you’re right you access those variables through the body. You will need body-parser middleware.

Comment posted by bgmn

I’m using

Comment posted by lndgalante

That should do the work, are you executing it before your endpoint definitions?, I’ll suggest creating another question for that case.