首页 > 解决方案 > 如何在直接管理员(Web 控制面板)上使用 node.js 后端部署我的 React 应用程序?

问题描述

我刚刚创建了以 node.js 作为后端的基本反应应用程序。在此我在同一端口上同时运行节点服务器和反应 Web 应用程序,并且它运行良好,但我不知道如何同时构建这两个应用程序......帮助我在cpanel中部署...

在节点中,我已连接到 SMTP 主机以发送邮件

server.js(node.js)

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var cors = require('cors');
const creds = require('./config');


var transport = {
    host: 'hsfm.in', // Don’t forget to replace with the SMTP host of your provider
    port: 587,
    auth: {
    user: creds.USER,
    pass: creds.PASS
  },
  tls: {
      // do not fail on invalid certs
      rejectUnauthorized: false
  },
}

var transporter = nodemailer.createTransport(transport)

transporter.verify((error, success) => {
  if (error) {
    console.log(error,"here");
  } else {
    console.log('Server is ready to take messages');
  }
});

router.post('/send', (req, res, next) => {
  var name = req.body.name
  var email = req.body.email
  var phone = req.body.phone
  var enquiry = req.body.enquiry
  var content = `Name: ${name} \n Email-ID: ${email} \n Phone: ${phone} \n Enquiry: ${enquiry} `

  var mail = {
    from: email,
    to: 'admin@hsfm.in',  // Change to email address that you want to receive messages on
    subject: 'New Enquiry Message from '+name ,
    text: content,
    html: content // html body
  }

  transporter.sendMail(mail, (err, data) => {
    if (err) {
      res.json({
        status: 'fail'
      })
    } else {
      res.json({
       status: 'success'
      })
    }
  })
})

const app = express()
app.use(cors())
app.use(express.json())
app.use('/', router)
app.listen(3001)

在节点中,我有用户同时在同一端口运行节点和反应应用程序

包.json(node.js)

{
  "name": "server",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node server.js",
    "build": "cd..  && npm run build",
    "client":"cd.. & npm start",
    "dev": "concurrently \"node server.js\" \"npm run client\""
  },
  "dependencies": {
    "concurrently": "^6.1.0",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1",
    "nodemailer": "^6.6.0"
  }
}

在反应 web 应用程序构建中如何提及 node.js ?

Package.json(反应应用)

{
  "name": "hsfm",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001/",
  "dependencies": {
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "concurrently": "^6.1.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "firebase": "^8.5.0",
    "nodemailer": "^6.6.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "web-vitals": "^1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
  },
  "description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
  "main": "index.js",
  "devDependencies": {},
  "repository": {
    "type": "git",
    "url": "."
  },
  "author": "dk",
  "license": "ISC",
  "homepage": "."
}

标签: javascriptnode.jsreactjsbackendweb-deployment

解决方案


推荐阅读