在reactjs中使用后端,联系表单

时间:2018-03-21 05:23:58

标签: javascript reactjs express nodemailer

我正在使用reactjs做我的第一个网站,我正在尝试填写联系表单页面,到目前为止我已经花了2天时间。我正在使用nodemailer,对于配置部分,我使用的是带有OAuth2的Gmail帐户。

用户将发送姓名,电子邮件和消息,表格将发送到我的电子邮件地址。在我按下发送'按钮网址更改为http://localhost:3000/contact(因为我已在package.json文件中定位)但页面中没有任何内容,甚至在控制台中也没有。

我不知道如何调试它并出现如下错误

  

' main.1932aa17.js:1未捕获的SyntaxError:意外的令牌<'。

如果有人帮忙,我会很高兴。

服务器文件夹

中的

index.js 文件

 import 'babel-polyfill'
 var express = require("express");
 import bodyParser from 'body-parser'

 import mailer from './mailer'

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

 app.get('*', (req, res) => {
  res.send('Server is working.')
 })

 app.post('/contact', (req, res) => {
  const { email = '', name = '', message = '' } = req.body

  mailer({ email, name, text: message }).then(() => {
    console.log(`Sent the message "${message}" from <${name}> ${email}.`);
    res.redirect('/#success');
  }).catch((error) => {
    console.log(`Failed to send this: "${message}" from <${name}> 
 ${email} with this error ${error && error.message}`);
    res.redirect('/#error');
   })
 })

 const PORT = process.env.PORT || 3000;

 app.listen(PORT, () => {
   console.log(`App is listening on port ${PORT}!`);
 })*

1 个答案:

答案 0 :(得分:0)

Node.js不支持import语句。您必须使用require

var bodyParser = require('body-parser')

require("babel-polyfill");

相关问题