微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Digitalocean 上部署 MERN Stack

如何解决如何在 Digitalocean 上部署 MERN Stack

我的服务器在以下端口上运行:

  • API:本地主机://5000
  • 前端:localhost://3000

我的 server.js 文件如下所示:

const __dirname = path.resolve()
app.use('/uploads',express.static(path.join(__dirname,'/uploads')))

if(process.env.NODE_ENV === 'production'){
    
  app.use(express.static(path.join(__dirname,'/frontend/build')))  

  app.get('*',(req,res) => {
      res.sendFile(path.resolve(__dirname,'frontend','build','index.html'))
  })
} else {
   app.get('/',res) => {
     res.send('App Is Running...')
   })
}


app.use(notFound)
app.use(errorHandler)

const PORT = process.env.PORT || 5000

app.use((req,res,next) => {
  res.header("Access-Control-Allow-Origin","*");
  res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept");
  next();
});

app.listen(5000,() => {
    console.log(`Server Is Running In ${process.env.NODE_ENV} Mode  On Port ${PORT}`.yellow.bold)
})

当我部署它时,它没有成功部署。

我买了一个 node.js 服务器并按照给我一个数字海洋的说明进行操作,

启动 server.js 文件时:

root@obstacle-sports-node:~/Obstacle-Sports# pm2 start backend/server.js
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/Obstacle-Sports/backend/server.js in fork_mode (1 instance)
[PM2] Done.
root@obstacle-sports-node:~/Obstacle-Sports# 

这是我的 Nginx 配置:

root /var/www/html/Obstacle-Sports;
# Add index.PHP to the list if you are using PHP
index index.html index.htm index.Nginx-debian.html;
server_name hellonode;
location / {

}
location /Obstacle-Sports/ {
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://localhost:5000;
}
}

尝试重新启动 Nginx 返回:

root@obstacle-sports-node:~/Obstacle-Sports# systemctl restart Nginx
Job for Nginx.service Failed because the control process exited with error code. See "systemctl status Nginx.service" and "journalctl -xe" for details.
root@obstacle-sports-node:~/Obstacle-Sports#

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。