即时通讯找不到404 API

时间:2019-03-13 22:22:20

标签: javascript node.js reactjs

我正在使用mailgun发送电子邮件,但出现404(未找到)错误。 这是我的工作

客户端

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JsonControllerIT {
    @LocalServerPort
    private int port;

    @Test
    public void jsonWithSerializationFeatureSet() {
        // given
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        messageConverter.setObjectMapper(objectMapper);
        restTemplate.getMessageConverters().removeIf(m -> m.getClass().getName().equals(MappingJackson2HttpMessageConverter.class.getName()));
        restTemplate.getMessageConverters().add(messageConverter);

        // when
        Detail detail = restTemplate.getForEntity("http://localhost:" + port + "/json", Detail.class).getBody();

        // then
        assertThat(detail.getSingleItemList().get(0)).isEqualTo(3);
    }
}

emailRouter.js

this.state= {
   message: "main message",
}

handeSendRequest(){
  await fetch('/api/eMail', {method:'post', headers: {'Content-type' : 'application/json' }}, body: JSON.stringify(this.state)){
}

}
render(){
  return(
    <Button onClick={this.handeSendRequest} > Send it </Button>
   )

 }

Server.js

const mailgun = require('mailgun-js')(configuration);

router.post('/api/eMail', async (req,res) => {
    try {

        let message = req.body.message;
      sendEmail(message,function (err) {
                    if(err){
                        console.log(err);
                    }else{
                        console.log("Sent email");
                    }

                });

 res.status(200).json({
            success: true,
            msg: 'Success'
        });
    }
    catch(err) {
        res.status(400).json({
            success: false,
            msg: err
        });
    }
});


function mail(message, callback){

  const mail = `<p>{message}</p>`;

    var mail = mailcomposer({
        from: 'from@email.com',
        to: "to@email.com",
        subject: "subject",
        html: mail
    });
    mail.build(function(mailBuildError, message) {

        var dataToSend = {
            to: to@email.com,
            message: message.toString('ascii')
        };

        mailgun.messages().sendMime(dataToSend, function (sendError, body) {
            if (sendError) {
                console.log(sendError);
            }
            callback(sendError);

        });
    });
}

不知道为什么,但是我遇到了这些错误const app = express(); const emailUtil = require('./emailRouter.js'); app.use('/api/eMail', emailUtil); 。看起来客户端是正确的,但是后端由于某种原因没有接到电话。如何修改我的工作,以便可以成功发送电子邮件。

1 个答案:

答案 0 :(得分:1)

如果您只是这样注册,该怎么办:app.use(emailUtil);  原因是,您在server.js和emailRouter.js

中都注册了/ api / eMail。

然后我认为,表达要您在/ api / eMail / api / eMail上请求