Express 服务器、后端、API [英] Express server, Back-End, API

查看:25
本文介绍了Express 服务器、后端、API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 三个月前,我自己在 YT 上开始使用 React.js.
  • 主要是获取 API
  • 某些 API 使用签名进行保护
  • 不知道我的 API 调用必须在服务器上,而不是在 React 中
  • 收到 CORS 错误
  • 不知道 cors 是什么以及如何修复 cors.
  • 开始学习后端

YT 上的每个视频:

  • JSON 占位符
  • SWAPI
  • 没有包含 React 和后端的视频,
  • 没有来自服务器视频的 POST 签名请求.
  • 差点放弃

CORS 错误解决方案:

  • 现在我知道如何修复 cors:
  • 使用代理在package.json中表达,
  • 在 src 中添加 setupProxy.js,
  • 和app.use(cors())"在 Express server.js 中

我的反应应用:

  • 24/7 全天候运行.
  • 需要每 3 秒从 API 更新一次状态.
  • 然后比较状态数据.(这是一些秘密公式和函数,如果可能的话,就不能向公众公开.在 express server.js 上隐藏代码)
  • 并采取相应的操作(新的 API 调用).

问题部分:

  • 这可能吗?
  • 如果网站上没有人,应用程序如何工作?
  • 这意味着 Express server.js 始终处于活动状态
  • 只有当有人打开网站查看数据发生了什么时,React UI 才存在
  • 如何使用 express 部署 react-app?这样好吗?:

https://dev.to/nburgess/creating-a-react-app-with-react-router-and-an-express-backend-33l3

  • 如何托管 - 在哪里部署?在 Github 页面上?
  • 快速服务器在部署时自动运行还是我需要npm start"?关于托管
  • 或首次打开应用,(打开网站)
  • 工作原理
  • 我需要django吗

什么更好:

  • 如果我决定与其他用户分享我的应用:
  • 给他们每个人一份 React 应用程序的副本,然后他们自己部署
  • 制作一个通用应用,以便每个用户都可以登录并拥有自己的带有仪表板和自己的数据和状态的快速服务器.
  • 如果通用应用程序更好,那就学习一下

推荐答案

服务器 24/7

当用户想要访问网站的某个页面时,服务器会将页面的蓝图(html、css 和 js,在这种情况下由您的 react js 构建代码构建)发送给它,这就是您的 react 应用程序出现的时候活着.

when a user wants to access a page of the website, the server sends it the page's blueprint (the html, css and js, which in this case is built by your react js build code), that's when your react app comes alive.

你已经有服务器端的express,你不需要django.

you already have express for server side, you don't need django.

是的,使用 heroku 进行部署很好.它将负责服务器,但您必须在服务器的 package.json 中添加一个脚本以使其构建反应,如果您尚未将反应构建上传到 heroku.

yea deploying with heroku is fine. it'll take care of the server but you'll have to add a script in the server's package.json to make it build react, if you're not already uploading a react build to heroku.

"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"

这里的客户端是您保存 React 代码的文件夹.

here client is the folder you keep your react code in.

如果它不应该是公开的,你就不应该把它作为反应状态.

If it's not supposed to be public, you shouldn't have it as a react state.

用通用的东西比较好,做一个以owner为权限的账号系统,url格式为www.website.com/owner'susername大功告成,每个所有者都会获得自己的网站版本.

Using a universal thing is better, just make an account system with owner as a privilege, and have the url in the form of www.website.com/owner'susername and done, every owner gets their own version of the website.

(youtube 上有一个很棒的 MERN 堆栈教程,由 traversy media 提供,可以回答您的大部分问题)

(there's an awesome MERN stack tutorial on youtube by traversy media that answers most of your questions)

这篇关于Express 服务器、后端、API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆