Pizhev Racing
Pizhev Racing

Reputation: 486

Is there a way to fix network 204 No content with Restful API in ReactJS

I try to create simple contact form with restful api and reactjs.

When I click on send button in the inspector I see two requests.

The first is:

Request URL: http://wwww.localhost:4000/api/v1/
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:4000
Referrer Policy: no-referrer-when-downgrade

The second is:

Request URL: http://wwww.localhost:4000/api/v1/
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:4000
Referrer Policy: no-referrer-when-downgrade

Before 3 weeks I've seen only one request in the network tab in the inspector with 200 OK.

My App.js file looks like that:

import React, { Component } from 'react';
import axios from 'axios';


class Contact extends Component {

    state = {
        name: '',
        message: '',
        email: '',
        username: '',
        password: '',
        sent: false,
        buttonText: 'Send Message'
    }

    formSubmit = (e) => {
        e.preventDefault()

        this.setState({
            buttonText: '...sending'
        })

        let data = {
            name: this.state.name,
            email: this.state.email,
            message: this.state.message,
            username: this.state.username,
            password: this.state.password
        }

        axios.post("http://wwww.localhost:4000/api/v1/", data)
        .then( res => {
            this.setState({ sent: true }, this.resetForm())
        })
        .catch( () => {
          console.log('Message not sent')
        })
      }

      resetForm = () => {
        this.setState({
            name: '',
            message: '',
            email: '',
            buttonText: 'Message Sent'
        })
    }

    render() {
        return(
          <form className="contact-form" onSubmit={ (e) => this.formSubmit(e)}>
          <label className="message" htmlFor="message-input">Your Message</label>
          <br/>
          <textarea onChange={e => this.setState({ message: e.target.value})} name="message" className="message-input" type="text" placeholder="Please write your message here" value={this.state.message} required/>
          <br/>
          <label className="message-name" htmlFor="message-name">Your Name</label>
          <br/>
          <input onChange={e => this.setState({ name: e.target.value})} name="name" className="message-name" type="text" placeholder="Your Name" value={this.state.name}/>
          <br/>
          <label className="message-email" htmlFor="message-email">Your Email</label>
          <br/>
          <input onChange={(e) => this.setState({ email: e.target.value})} name="email" className="message-email" type="email" placeholder="[email protected]" required value={this.state.email} />

          <div className="button--container">
              <button type="submit" className="button button-primary">{ this.state.buttonText }</button>
          </div>
        </form>
        );
    }
}

export default Contact;

My Api.js file looks like that:

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const cors = require('cors');

const app = express();

const port = 4000;

app.use(cors());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req, res) => {
  res.send('Welcome to my api');
})

app.post('/api/v1/', (req,res) => {
  const { body: { username, password }} = req;
  const params = req.params;
  console.log(params)
  var data = req.body;

var smtpTransport = nodemailer.createTransport({
  service: 'Gmail',
  port: 465,
  auth: {
    user: '[email protected]',
    pass: 'passwordEmail'
  }
});

var mailOptions = {
  from: data.email,
  to: '[email protected]',
  subject: 'New Request',
  html: `<p>${data.name}</p>
          <p>${data.email}</p>
          <p>${data.message}</p>`
};

smtpTransport.sendMail(mailOptions,
(error, response) => {
  if(error) {
    res.send(error)
  }else {
    res.send('Success')
  }
  smtpTransport.close();
});

})

app.listen(port, () => {
  console.log('We are live on port 4000');
});

So I dont know what means 204 No content.. but the e-mail not comming..

Can I take some hint how to fix this problem.. ?

Upvotes: 0

Views: 4807

Answers (1)

Ernesto
Ernesto

Reputation: 4272

the "options" request look at this response How to filter (hide) Pre-flight requests on my Dev Tools Network

but your problem lives here

...
  formSubmit = (e) => {
        e.preventDefault()

        this.setState({
            buttonText: '...sending'
        })

        const body = JSON.stringify({
            name: this.state.name,
            email: this.state.email,
            message: this.state.message,
            username: this.state.username,
            password: this.state.password
        })

       return axios.post("http://wwww.localhost:4000/api/v1/", {data: body})
        .then(() => this.setState({ ...this.resetForm(), sent: true }))
        .catch(console.log)
      }

      resetForm = () => ({
            name: '',
            message: '',
            email: '',
            buttonText: 'Message Sent'
        });
...

also there are quite a few problems on the back

Upvotes: 1

Related Questions