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

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

问题描述

我是新来的.

  • 三个月前,我在YouTube上以React.js开头.

  • 主要是获取API

  • 某些API由签名保护
  • 不知道我的API调用必须在服务器上,而不是在React中

  • 获取CORS错误
  • 不知道是什么以及如何修复cors.

  • 开始学习后端

YouTube上的每个视频:

  • JSON占位符
  • SWAPI

  • 没有带有React和后端的视频
  • 没有来自服务器视频的POST签名请求.
  • 几乎放弃

CORS错误解决方案:

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

我的应用程序:

  • 运行24/7.
  • 需要每3秒钟从API更新一次状态.
  • 然后比较状态数据. (这是一些秘密的公式和函数,即使有可能也无法公开.要在快速的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页面上?
  • 在部署时自动运行Express服务器,还是需要"npm start"?在托管上
  • 或首次打开该应用(打开网站)
  • 如何运作
  • 我需要Django

哪个更好:

  • 如果我决定与其他用户共享我的应用程序:
  • 给他们每个人一份react应用的副本,然后他们自己进行部署
  • 制作一个通用应用程序,这样每个用户都可以登录并拥有自己的带有仪表板以及数据和状态的Express服务器.

  • 如果通用应用程序更好,那就去学习


谢谢您的任何回答.


解决方案

服务器全天候运行24/7

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

您已经在服务器端表示快递了,不需要Django.

是的,使用heroku部署是可以的.它会照顾服务器,但是如果您还没有将react build上载到heroku,则必须在server的package.json中添加脚本以使其构建react.

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

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

如果不应该将其公开,则不应将其作为反应状态.

使用通用的方法更好,只需建立一个拥有者作为特权的帐户系统,并以 www.website.com/owner的用户名 完成后,每个所有者都会获得自己的网站版本.

(在YouTube上,遍历媒体提供了一个很棒的MERN堆栈教程,可以回答您的大多数问题)

Hi, I'm new.

  • started with React.js, by my self, three months ago, on YT.

  • mostly fetching API

  • some API's are secured with a signature
  • didn't know that my API calls must be on the server, not in React

  • getting CORS error's
  • didn't know what is and how to fix cors.

  • started to learning a back-end

every video on YT:

  • JSON placeholder
  • SWAPI

  • no video with React and back-end,
  • no POST signed request from server video.
  • almost give up

CORS error solution:

  • now I know how to fix cors:
  • with proxy to express in package.json,
  • adding setupProxy.js in src,
  • and "app.use(cors())" in Express server.js

My react app:

  • runs 24/7.
  • need to update the states every 3s from API.
  • then compare states-data. (that is some secret formulas and functions that cannot be exposed to the public if that's even possible. To hide code on the express server.js)
  • and take corresponding actions (new API calls).

Question part:

  • is that even possible?
  • how the app is working if no one is on the website?
  • this means that the Express server.js is always alive
  • and React UI is alive only when someone open website to see what's going on with data

  • how to deploy react-app with express? is this good?:

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


  • how to host - where to deploy? On Github pages?
  • do express server autoruns on deploy or do I need "npm start" on hosting
  • or open the app for the first time, (open website)
  • how that works
  • do I need django

what is better:

  • if I decide to share my app with other users:
  • to give them each a copy of react app, and they deploy themselves
  • or
  • make a universal app so each user can log in and have their own express server with dashboard and their own data and states.

  • if the universal app is better were to learn that


Thank you for any question that is answered.


解决方案

Server runs 24/7

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.

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

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"

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.

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.

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

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

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